Search code examples
pythonepochdayofweek

Convert epoch to the week day without for loop


With the help of the following code I can define day of the week out of epoch:

epoch = 4464769126
day = time.strftime('%A', time.localtime(epoch))
print(day)

Friday

I have applied it to the column in df.epoch with the for loop, but it is time consuming. Any ideas how to speed up the process?


Solution

  • you do this using pandas to_datetime function like below

    import pandas as pd
    df = pd.DataFrame({"epoch": ["4464769126"]})
    
    pd.to_datetime(df["epoch"], unit="s").dt.weekday_name