28

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.

Shawn Schreier
  • 780
  • 2
  • 10
  • 20
  • In your second code snippet, you need to convert to datetime first (what you did in the first one). – FObersteiner Jul 09 '20 at 06:28
  • 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? – Shawn Schreier Jul 09 '20 at 12:24
  • You can remove date.dt inside the functions, but outside use df['Date'].date – 3Mcollab Jun 01 '21 at 04:59

3 Answers3

46

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://stackoverflow.com/a/48967889/13720936

Jerry wu
  • 802
  • 8
  • 17
9

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.

Shawn Schreier
  • 780
  • 2
  • 10
  • 20
  • 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 syntax – Golden Lion Jan 08 '21 at 16:03
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))
Golden Lion
  • 3,840
  • 2
  • 26
  • 35