Search code examples
pandasdatetimetimestamp

Error converting timestamp to date in Pandas with dt accessor


Here is my example:

import pandas as pd
import datetime
my_df = pd.DataFrame({'date_clmn': [datetime.date(2020, 1, 1),
                                    datetime.date(2020, 2, 1),
                                    datetime.date(2020, 3, 1)]})
my_df['lag_date'] = my_df.date_clmn - pd.DateOffset(months = 3)
my_df.lag_date.dt.date

The last line generates error:

AttributeError: Can only use .dt accessor with datetimelike values

As far as I understand dt accessor should work with timestamps. Why is it producing an error message?

Update

To confirm data type I ran:

my_df.lag_date.apply(type)

It returned:

0    <class 'pandas._libs.tslibs.timestamps.Timesta...
1    <class 'pandas._libs.tslibs.timestamps.Timesta...
2    <class 'pandas._libs.tslibs.timestamps.Timesta...
Name: lag_date, dtype: object

Solution

  • I think there is problem dates are converting to datetimes, but in loop, so finally is not created TimeSeries, but objects of Timestamps.

    my_df['lag_date'] = my_df.date_clmn - pd.DateOffset(months = 3)
    print (my_df.lag_date.apply(type))
    0    <class 'pandas._libs.tslibs.timestamps.Timesta...
    1    <class 'pandas._libs.tslibs.timestamps.Timesta...
    2    <class 'pandas._libs.tslibs.timestamp
    

    Possible solution is convert datess to datetimes first:

    my_df['lag_date'] = pd.to_datetime(my_df.date_clmn) - pd.DateOffset(months = 3)
    print (my_df.lag_date.dt.date)
    0    2019-10-01
    1    2019-11-01
    2    2019-12-01
    Name: lag_date, dtype: object
    

    Or last:

    my_df['lag_date'] = pd.to_datetime(my_df.date_clmn - pd.DateOffset(months = 3))
    print (my_df.lag_date)
    0   2019-10-01
    1   2019-11-01
    2   2019-12-01
    Name: lag_date, dtype: datetime64[ns]