How to slice date from a timestamp and convert to string?
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: aux1.append(str(i)) 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:
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
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: date_list.append(i.strftime('%Y-%m-%d'))
DatetimeIndex.strftime with converting to
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 = datetime.now() dateTimeObj = datetime.now () dateTimeObj = datetime.now () Convert this datetime object to string in format ‘ DD-MMM-YYYY (HH:MM:SS:MICROS)’ i.e.
You can use
aux1 =  for i in time_list: aux1.append(i.dt.strftime('%Y-%m-%d')) date_list = aux1
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() print(dates)
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.
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.
'Timestamp' object is not subscriptablebut the string is!
- I preferred your third approach because it does not have any
forloop. This worked perfectly. Thanks a ton. Can you clarify my doubt, that is, using
forloop 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