Home > Software engineering >  pd.to_datetime gives Timestamp objects and to_pydatetime does not work on it
pd.to_datetime gives Timestamp objects and to_pydatetime does not work on it

Time:04-27

I am reading a dataframe:

df = pd.read_csv("file_path")
df['time'] = pd.to_datetime(df['time'], format="%Y-%m-%d")

When I want to access df['time'].dt in order to get year, month, etc. I get the error that:

'Timestamp' object has no attribute 'dt'.

There are many posts on StackOverflow related to my question. I tried the suggested solutions which was using to_pydatetime() but it did not work. I appreciate any help regarding this.

CodePudding user response:

In order to get/extract a parameter like hour or year of a Timestamp data type in pandas, you can use the methods with the name of the attribute.

Example:

  • df['time].year
  • df['time].month
  • df['time'].day
  • df['time].hour

ref: https://pandas.pydata.org/docs/reference/api/pandas.Timestamp.hour.html https://pandas.pydata.org/docs/reference/api/pandas.Timestamp.year.html

CodePudding user response:

OK, then, I'm confused about what code you actually have. The error message you cite says that you have a Timestamp object, not a Series. IF you have a Series, then you need dt. If you have an individual element, then you do not need dt. Here is a snippet based on your code that works perfectly. If this is not what you have, then you need to modify the question.

import pandas as pd
data = [
        '2019-06-06',
        '2020-04-01',
        '2021-01-01'
    ]

df = pd.DataFrame(data, columns=['time'] )
print(df)
df['time'] = pd.to_datetime(df['time'], format="%Y-%m-%d")
print(df)
print(df['time'].dt.year)

Output:

         time
0  2019-06-06
1  2020-04-01
2  2021-01-01
        time
0 2019-06-06
1 2020-04-01
2 2021-01-01
0    2019
1    2020
2    2021
Name: time, dtype: int64
  • Related