Sigal Cohen
Sigal Cohen

Reputation: 21

Get day of week from pandas Series of epochs

I'm trying to convert a column of epoch time to the day of the week. I have a Dataframe called df and my code is:

import pandas as pd
import time

df = pd.read_csv('df.csv')

df['epoch'] = (df['epoch']).to_string()
df['day'] = time.ctime(df['epoch'])

I tried astype() but it's not working.

So assuming I have a column (from a large data) of epoch time containing:

1536444331894
1536444363932
1536444386908
1540436500055
1536444443458

and so on...

I need the day in this column instead of the seconds:

Saturday
Saturday
Saturday
Thursday
Saturday
...

Upvotes: 0

Views: 400

Answers (2)

Nakor
Nakor

Reputation: 1514

I think you might be looking for pandas.to_datetime()

Combined with the dayofweek attribute.

You would need to do:

pd.to_datetime(df['epoch']).dt.dayofweek

Upvotes: 1

cs95
cs95

Reputation: 402333

Pandas provides a vectorized method to_datetime for datetime conversion. Your epochs are in milliseconds, so you'll need to pass a unit='ms' argument accordingly. From there, you can use strftime to extract the day.

pd.to_datetime(df['epoch'], unit='ms').dt.strftime('%A')

0    Saturday
1    Saturday
2    Saturday
3    Thursday
4    Saturday
Name: epoch, dtype: object

Upvotes: 3

Related Questions