简体   繁体   中英

Get day of week from UTC_TIME Series in Pandas

When I do

df.dtypes

, appears this

utc_time         int64

one example of utc_time is: 1536444323321

I found a code here to change utc_time (epoch) to day of week

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

But i receive this error:

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'>

First, convert your column/Series to a datetime object. df['column'] = pd.to_datetime(df['column'])

Then, you have two options:

  • Option 1 - use the .dt accessor: df['column'].dt.weekday gives you the weekdays as integers

  • Option 2 - use the pandas Timestamp object: df['column'].apply(pd.Timestamp.weekday)

But I really recommend option 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM