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.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With