简体   繁体   中英

Convert datetime ns to daily format

I have a column in my dataframe in this formate:

2013-01-25 00:00:00+00:00

non-null datetime64[ns, UTC]

I would like to convert this to daily format, like this:

2013-01-25

I tried this approach, but have been receiving an error:

df['date_column'].date()

AttributeError: 'Series' object has no attribute 'date'

The error message is not quite clear to me, because the object should be a datetime object according to df.info()

Can anyone suggest an approach of how to do this?

In short : It is not advisable to convert to date objects, since then you lose a lot of functionality to inspect the date s. It might be better to just dt.floor(..) [pandas-doc] , or dt.normalize(..) [pandas-doc] .

You can convert a series of strings with pd.to_datetime(..) [pandas-doc] , for example:

>>> pd.to_datetime(pd.Series(['2013-01-25 00:00:00+00:00']))
0   2013-01-25
dtype: datetime64[ns]

We can then later convert this to date objects with .dt.date [pandas-doc] :

>>> pd.to_datetime(pd.Series(['2013-01-25 00:00:00+00:00'])).dt.date
0    2013-01-25
dtype: object

Note that a date is not a native Numpy type, and thus it will use a Python date(..) object. A disadvantage of this is that you can no longer process the objects are datetime-like objects. So the Series more or less loses a lot of functionality.

It might be better to just dt.floor(..) [pandas-doc] to the day, and thus keep it a datetime64[ns] object:

>>> pd.to_datetime(pd.Series(['2013-01-25 00:00:00+00:00'])).dt.floor(freq='d')
0   2013-01-25
dtype: datetime64[ns]

We can use dt.normalize(..) [pandas-doc] as well. This just sets the time component to 0:00:00 , and leaves the timezone unaffected:

>>> pd.to_datetime(pd.Series(['2013-01-25 00:00:00+00:00'])).dt.normalize()
0   2013-01-25
dtype: datetime64[ns]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM