0

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.

Moody
  • 35
  • 1
  • 8

3 Answers3

2

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

1

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()])
Daniel Trugman
  • 8,186
  • 20
  • 41
  • So I have a dataframe that looks like this: -------------------------- Load-kW Time 2021-03-23 04:59:00-07:00 57.355556 2021-03-23 05:00:00-07:00 61.095000 2021-03-23 05:01:00-07:00 61.260000 2021-03-23 05:02:00-07:00 61.868333 2021-03-23 05:03:00-07:00 61.378333 ----------------------/ and its shape is (619663, 2). How to find the day of the week for every corresponding data point? – Moody Jun 21 '22 at 13:47
0

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

Moody
  • 35
  • 1
  • 8
  • There was good information in this thread as well: https://stackoverflow.com/questions/49198068/how-to-remove-timezone-from-a-timestamp-column-in-a-pandas-dataframe and this YouTube video: https://www.youtube.com/watch?v=UFuo7EHI8zc – Moody Jun 21 '22 at 15:50