Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of dates from DatetimeIndex object

I have a DatetimeIndex object comprised of two dates given as follows:

import pandas as pd
timestamps = pd.DatetimeIndex(['2014-1-1', '2014-1-2'], freq='D')

which looks like this:

DatetimeIndex(['2014-01-01', '2014-01-02'], dtype='datetime64[ns]', freq='D')

How do I get a list of dates from the timestamps object? i.e. I want the following:

['2014-01-01', '2014-01-02']
like image 716
alexjrlewis Avatar asked Jan 26 '23 09:01

alexjrlewis


2 Answers

to_native_types() converts the values of timestamps to str format and then tolist() creates a list of str (dates).

timestamps.to_native_types().tolist()

Output

['2014-01-01', '2014-01-02']
like image 123
vb_rises Avatar answered Jan 28 '23 22:01

vb_rises


Can you try the following:

timestamps.strftime('%Y-%m-%d').tolist()

Output:

['2014-01-01', '2014-01-02']
like image 42
Jeril Avatar answered Jan 28 '23 22:01

Jeril