Search code examples
pythonpandaspython-datetimepandas-datareader

'DatetimeProperties' object has no attribute 'isocalendar'


I have a dataset with trips and their respective start and end times. For analyzing that data I want to add the month, week, day and hour of the start time in my dataframe.

Here is that part of my script:

    data["start_time"] = pd.to_datetime(bike_data["start_time"], infer_datetime_format=True)
    data["start_time"] = pd.to_datetime(bike_data["start_time"], format="%Y/%m/%d %H:%M:%S")


    data["start_month"] = bike_data["start_time"].dt.month
    data["start_day"] = bike_data["start_time"].dt.day
    data["start_hour"] = bike_data["start_time"].dt.hour
    data["start_week"] = bike_data["start_time"].dt.isocalendar().week
    data["weekday"] = bike_data["start_time"].dt.weekday + 1
    data["rounded_time"] = bike_data["start_time"].dt.round('30min')

I get the error:'DatetimeProperties' object has no attribute 'isocalendar'

I tried using the isocalendar method on a single date and it seems to work. I'm also sure that I have imported everything necessary.


Solution

  • Probably, your pandas version is too old. pandas.Series.dt.isocalendar (which is what you're trying to use) was added in version 1.1.0 (PR #33220).

    You'll need to either update your pandas version to 1.1.0 or later, or use .dt.week, e.g.

    data["start_week"] = bike_data["start_time"].dt.week