添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
会开车的紫菜  ·  “‘Get ...·  10 月前    · 
老实的玉米  ·  python - Class for ...·  1 年前    · 
空虚的钱包  ·  Oracle ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

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
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.