I have a dataframe with the following format:
datetime.datetime(2021, 3, 23, 4, 59, tzinfo=tzoffset(None, -25200))
How can I find the day name? I tried .day_name()
, but for some reason when you have the zone data that doesn't work.
I have a dataframe with the following format:
datetime.datetime(2021, 3, 23, 4, 59, tzinfo=tzoffset(None, -25200))
How can I find the day name? I tried .day_name()
, but for some reason when you have the zone data that doesn't work.
Try using
object.strftime("%A")
where object refer to instance of datetime
This will return the day name as in name of days in week
You can use weekday
to get a numeric day of the week. You can then use a dictionary to translate that to actual day names. Something like this:
from datetime import datetime
from dateutil.tz import tzoffset
date = datetime(2021, 3, 23, 4, 59, tzinfo=tzoffset(None, -25200))
days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
print(days[date.weekday()])
Here is what I did and it worked for me. Thanks for the aforementioned solution, it works on a single data point, but for a dataframe here is what I did: say you have a dataframe df with the data in column 'Time', then:
df['Time']=df['Time'].apply(lambda x: x.replace(tzinfo=None)) # that takes out the zone time
df['Time'] = pd.to_datetime(df['Time']) # to convert it to dtype
import datetime as dt
df['DayofWeek'] = df['Time'].dt.day_name() # make a new column with day name