【问题标题】:Converting Time Stamps in Pandas Dataframe to read as corresponding weekday将 Pandas Dataframe 中的时间戳转换为相应的工作日
【发布时间】:2021-07-07 23:39:51
【问题描述】:

我在 pandas 数据框中有一个包含 20,000 个时间戳的列表。有没有可以将它们转换为相应工作日的功能?我有这样的,如果我做 df['date'],所有的时间戳都会填充。

【问题讨论】:

标签: python pandas date datetime time


【解决方案1】:

确保该列已转换为to_datetime()(如果df.date.dtype 已显示datetime 类型,则可以跳过此步骤):

df = pd.DataFrame({'date': ['2021-01-01', '2021-01-02']})
df.date = pd.to_datetime(df.date)

#          date
# 0  2021-01-01
# 1  2021-01-02

然后正如克里斯蒂安所说,使用dt accessor

df.date.dt.day_name()

# 0      Friday
# 1    Saturday
# Name: date, dtype: object
df.date.dt.weekday

# 0    4
# 1    5
# Name: date, dtype: int64

【讨论】:

    猜你喜欢
    • 2018-10-09
    • 2017-05-18
    • 2018-09-11
    • 2016-10-05
    • 2021-01-18
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    • 2019-09-16
    相关资源
    最近更新 更多