Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

looking for Pandas.DateTimeIndex.is_dst()

Tags:

python

pandas

I have a DateFrame with a DateTimeIndex, i.e.

import pandas as pd
dates = pd.date_range('2018-04-01', periods=96, freq='15T', tz='Australia/Sydney', name='timestamp')
df = dates.to_frame(index=False)
df.set_index(dates.name, inplace=True)

I want to create a column with an 0/1 indicator column which is 1 during summer time and 0 during winter, but I cannot find the relevant dst / is_dst attribute, i.e. I want something like

df['is_dst'] = df.index.is_dst()

can anyone advise that the correct method / property is. Or Do I need to covert to a different 'datetime' class?

I need something general - i.e. work for any timezone including say 'Australia/Brisbane' which doesn't have daylight savings. I'd prefer not to have to parse out the timezone offset and try and determine if it's summer / winter.

like image 802
David Waterworth Avatar asked Jan 01 '23 15:01

David Waterworth


1 Answers

It have in pandas

df.index.map(lambda x : x.dst())

After a small change can yield the Boolean

df.index.map(lambda x : int(x.dst().total_seconds()!=0))
Out[104]: 
Int64Index([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0],
           dtype='int64', name='timestamp')
like image 142
BENY Avatar answered Jan 12 '23 13:01

BENY