Timestamp object has no attribute dt
Asked Answered
W

4

34

I am trying to convert a new column in a dataframe through a function based on the values in the date column, but get an error indicating "Timestamp object has no attribute dt." However, if I run this outside of a function, the dt attribute works fine.

Any guidance would be appreciated.

This code runs with no issues:

sample = {'Date': ['2015-07-02 11:47:00', '2015-08-02 11:30:00']}
dftest = pd.DataFrame.from_dict(sample)
dftest['Date'] = pd.to_datetime(dftest['Date'])
display(dftest.info())
dftest['year'] = dftest['Date'].dt.year
dftest['month'] = dftest['Date'].dt.month

This code gives me the error message:

sample = {'Date': ['2015-07-02 11:47:00', '2015-08-02 11:30:00']}
dftest = pd.DataFrame.from_dict(sample)
dftest['Date'] = pd.to_datetime(dftest['Date'])
def CALLYMD(dftest):
    if dftest['Date'].dt.month>9:
        return str(dftest['Date'].dt.year) + '1231'
    elif dftest['Date'].dt.month>6: 
        return str(dftest['Date'].dt.year) + '0930'
    elif dftest['Date'].dt.month>3: 
        return str(dftest['Date'].dt.year) + '0630'
    else:
        return str(dftest['Date'].dt.year) + '0331'
    

dftest['CALLYMD'] = dftest.apply(CALLYMD, axis=1)

Lastly, I'm open to any suggestions on how to make this code better as I'm still learning.

Witching answered 8/7, 2020 at 21:5 Comment(4)
In your second code snippet, you need to convert to datetime first (what you did in the first one).Wallpaper
Sorry about that - in my original code it is already a datetime object. I just forgot that in my second code but have edited the post to include it. Adding that in still does not fix the AttributeError that is returned. Any other thoughts?Witching
You can remove date.dt inside the functions, but outside use df['Date'].dateHoudon
You didn't write the error message, but basically you only need to use dt on non-index columns. On indexes you can omit itProto
P
56

I'm guessing you should remove .dt in the second case. When you do apply it's applying to each element, .dt is needed when it's a group of data, if it's only one element you don't need .dt otherwise it will raise {AttributeError: 'Timestamp' object has no attribute 'dt'}

reference: https://mcmap.net/q/451292/-applying-function-to-dataframe-timestamp-dt

Powerdive answered 7/8, 2020 at 3:5 Comment(0)
W
10

After looking at the timestamp documentation, I found removing the .dt and just doing .year and .month works. However, I'm still confused as to why it works in the first code but does not work in the second code.

Witching answered 9/7, 2020 at 12:43 Comment(1)
year=pd.to_datetime(item['CompleteOrExpireDate']).year I was surprised but removing dt worked. if the data frame column is datetime then using dt.year is correct syntaxStier
S
3

here is how to create a yearmonth bucket using the year and month

for key, item in df.iterrows(): 
     year=pd.to_datetime(item['Date']).year
     month=str(pd.to_datetime(item['Date']).month)
     df.loc[key,'YearMonth']="{:.0f}{}".format(year,month.zfill(2))
Stier answered 8/1, 2021 at 16:24 Comment(0)
N
0
import pandas as pd
import numpy as np

sample = {'Date': ['2015-07-02 11:47:00', '2015-08-02 11:30:00']}
dftest = pd.DataFrame.from_dict(sample)
dftest['Date'] = pd.to_datetime(dftest['Date'])

conditions = [
    dftest['Date'].dt.month > 9,
    dftest['Date'].dt.month > 6,
    dftest['Date'].dt.month > 3
]
choices = [str(dftest['Date'].dt.year) + '1231',
            str(dftest['Date'].dt.year) + '0930',
            str(dftest['Date'].dt.year) + '0630']
dftest['CALLYMD'] = np.select(conditions, choices, default=str(dftest['Date'].dt.year) + '0331')


"""
print(dftest.to_string())
                 Date                                             CALLYMD
0 2015-07-02 11:47:00  0    2015\n1    2015\nName: Date, dtype: int320930
1 2015-08-02 11:30:00  0    2015\n1    2015\nName: Date, dtype: int320930"""
Narcotic answered 23/2, 2024 at 10:43 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.