How to slice date from a timestamp and convert to string?

convert string to datetime python
extract date from timestamp python
timestamp to date
convert timestamp to date python
pandas timestamp to datetime
convert timestamp to date python pandas
pandas convert string to datetime

My list of timestamps is given below:

time_list = 
[Timestamp('2019-01-24 00:00:00'),
 Timestamp('2019-01-27 00:00:00'),
 Timestamp('2019-01-29 00:00:00'),
 Timestamp('2019-02-08 00:00:00'),
 Timestamp('2019-02-09 00:00:00'),
 Timestamp('2019-02-10 00:00:00')]

I would like to take only the date part and convert it to a string. My code is

aux1 = []
for i in time_list:     
date_list = aux1

Present output is:

date_list = 
['2019-01-24 00:00:00',
 '2019-01-27 00:00:00',
 '2019-01-29 00:00:00',
 '2019-02-08 00:00:00',
 '2019-02-09 00:00:00',
 '2019-02-10 00:00:00']

However the output I want is:

date_list = ['2019-01-24', '2019-01-27', '2019-01-29', '2019-02-08', '2019-02-09', '2019-02-10']

In my code I have used for loop approach but it is not producing the desired answer. Is there a better approach to get my desired date_list?

Use Timestamp.strftime in list comprehension or loop:

date_list = [x.strftime('%Y-%m-%d') for x in time_list]

date_list = []
for i in time_list:     

Or use DatetimeIndex.strftime with converting to list:

date_list = pd.DatetimeIndex(time_list).strftime('%Y-%m-%d').tolist()
print (date_list)
['2019-01-24', '2019-01-27', '2019-01-29', '2019-02-08', '2019-02-09', '2019-02-10']

Python Datetime Tutorial: Manipulate Times, Dates, and Time Spans, strptime() can read strings with date and time information and convert them to datetime objects, and strftime() converts datetime objects back into� from datetime import datetime. Let’s use it to convert datetime object to string. Example 1: Get the current timestamp in a datetime object i.e. dateTimeObj = dateTimeObj = () dateTimeObj = () Convert this datetime object to string in format ‘ DD-MMM-YYYY (HH:MM:SS:MICROS)’ i.e.

You can use dt.strftime


aux1 = []
for i in time_list:     
date_list = aux1

Date and time, An integer number representing the number of milliseconds that has passed since If there is a single argument, and it's a string, then it is parsed automatically. Returns the timestamp for the date – a number of milliseconds passed let time2 = 0; // run bench(upperSlice) and bench(upperLoop) each 10 � In this blog article, you’ll learn ways to convert timestamp to Date format. convert timestamp string to Date. timestamp is a unix format used to holds date and time and It is 10 digit number. How to get the timestamp in Javascript

Try like this:

import dateutil.parser

date_list = ['2019-01-24 00:00:00',
 '2019-01-27 00:00:00',
 '2019-01-29 00:00:00',
 '2019-02-08 00:00:00',
 '2019-02-09 00:00:00',
 '2019-02-10 00:00:00']

for data in date_list:

   dates = dateutil.parser.parse(data).date()

Time Series / Date functionality — pandas 0.25.0.dev0+752 , Resampling or converting a time series to a particular frequency In addition to the required datetime string, a format argument can be passed to If the timestamp string is treated as a slice, it can be used to index DataFrame with [] as well. The toUTCString() method is used to represent the Date object as a string the UTC time format. The time from this date string can be found by extracting from the 11th to last to the 4th to the last character of the string. This is extracted using the slice() function. This string is the time representation of the UNIX timestamp. Syntax:

Time Series / Date functionality — pandas 0.23.1 documentation, pandas allows you to capture both representations and convert between them. In addition to the required datetime string, a format argument can be passed to If the timestamp string is treated as a slice, it can be used to index DataFrame� First argument is the string format of the date and time. Second argument is the format of the input string. We convert date and time string into a date object. Then we use timetuple () to convert date object into tuple. At the end we use mktime (tuple) to convert the Date tuple into a timestamp.

strptime: Date-time Conversion Functions to and from Character, A character string specifying the time zone to be used for the conversion. rest of the C-level datetime code) by code modified from IANA's tzcode distribution� Javascript however provides multiple functions to present date and time in human readable form. They are: toDateString. Converts the date portion of a Date object into a readable string. toGMTString. Deprecated. Use the toUTCString () method instead. toISOString. Returns the date as a string, using the ISO standard.

Basic Time Series Manipulation with Pandas, create a date range � work with timestamp data � convert string data to a timestamp � index and slice your time series data in a data frame � resample� Calling Function strptime () Now we have to call a function strptime () that creates a datetime object from a string representing a date and time and a corresponding format string. It takes two argument one is string representation of datetime and another one is pattern of the input string.

  • aux1.append(i.dt.strftime('%Y-%m-%d')) ?
  • try str(i)[:10] apparently 'Timestamp' object is not subscriptable but the string is!
  • I preferred your third approach because it does not have any for loop. This worked perfectly. Thanks a ton. Can you clarify my doubt, that is, using for loop is it a bad code?
  • @Msquare - It depends, but short answer is dont use loops, if exist vectorized alternative. More info about this is here