从Pandas的UTC_TIME系列获取星期几

问题描述 投票:1回答:1

当我做

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个回答
1
投票

首先,将列/ Series转换为datetime对象。 df['column'] = pd.to_datetime(df['column'])

然后,您有两个选择:

  • 选项1 - 使用.dt访问器:df['column'].dt.weekday为工作日提供整数
  • 选项2 - 使用pandas Timestamp对象:df['column'].apply(pd.Timestamp.weekday)

但我真的推荐选项1。

© www.soinside.com 2019 - 2024. All rights reserved.