【问题标题】:Get day of week from UTC_TIME Series in Pandas从 Pandas 中的 UTC_TIME 系列获取星期几
【发布时间】:2019-08-10 02:01:49
【问题描述】:

当我这样做时

df.dtypes

,出现这个

utc_time         int64

utc_time 的一个例子是: 1536444323321

我在这里找到了将 utc_time (epoch) 更改为星期几的代码

df['Week_Day'] = 
datetime.fromtimestamp(df['utc_time']/1000).strftime("%A")

但我收到此错误:

TypeError                                 Traceback (most recent call 
last)
<ipython-input-124-b3277c232078> in <module>()
      2 # df['intage'] = df['utc_time'].astype(int)
      3 # df['intage'] = df['utc_time'].dt.days
----> 4 df['Week_Day'] = 
datetime.fromtimestamp(df['utc_time']/1000).strftime("%A")

/anaconda3/lib/python3.6/site-packages/pandas/core/series.py in 
wrapper(self)
    115             return converter(self.iloc[0])
    116         raise TypeError("cannot convert the series to "
--> 117                         "{0}".format(str(converter)))
    118 
    119     return wrapper

TypeError: cannot convert the series to <class 'int'>

【问题讨论】:

    标签: python pandas utc dayofweek


    【解决方案1】:

    首先,将您的列/系列转换为日期时间对象。 df['column'] = pd.to_datetime(df['column'])

    那么,你有两个选择:

    • 选项 1 - 使用 .dt 访问器: df['column'].dt.weekday 以整数形式为您提供工作日

    • 选项 2 - 使用 pandas Timestamp 对象: df['column'].apply(pd.Timestamp.weekday)

    但我真的推荐选项 1。

    【讨论】:

    • 如果你喜欢我的回答,请不要忘记接受它:)
    猜你喜欢
    • 1970-01-01
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    • 2017-11-07
    • 1970-01-01
    相关资源
    最近更新 更多