looking for Pandas.DateTimeIndex.is_dst()
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.
python pandas
add a comment |
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.
python pandas
Maybe this can help your case: stackoverflow.com/questions/44124436/python-datetime-to-season
– Sanchit Kumar
Nov 14 '18 at 2:13
Thanks @SanchitKumar, that's useful although I prefer the accepted answer as it gives me the datetime information. The answer you link assumes summer / winter transition is start / end of month
– David Waterworth
Nov 14 '18 at 3:06
add a comment |
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.
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.
python pandas
python pandas
edited Nov 14 '18 at 2:09
Brad Solomon
13.5k73484
13.5k73484
asked Nov 14 '18 at 2:06
David WaterworthDavid Waterworth
568416
568416
Maybe this can help your case: stackoverflow.com/questions/44124436/python-datetime-to-season
– Sanchit Kumar
Nov 14 '18 at 2:13
Thanks @SanchitKumar, that's useful although I prefer the accepted answer as it gives me the datetime information. The answer you link assumes summer / winter transition is start / end of month
– David Waterworth
Nov 14 '18 at 3:06
add a comment |
Maybe this can help your case: stackoverflow.com/questions/44124436/python-datetime-to-season
– Sanchit Kumar
Nov 14 '18 at 2:13
Thanks @SanchitKumar, that's useful although I prefer the accepted answer as it gives me the datetime information. The answer you link assumes summer / winter transition is start / end of month
– David Waterworth
Nov 14 '18 at 3:06
Maybe this can help your case: stackoverflow.com/questions/44124436/python-datetime-to-season
– Sanchit Kumar
Nov 14 '18 at 2:13
Maybe this can help your case: stackoverflow.com/questions/44124436/python-datetime-to-season
– Sanchit Kumar
Nov 14 '18 at 2:13
Thanks @SanchitKumar, that's useful although I prefer the accepted answer as it gives me the datetime information. The answer you link assumes summer / winter transition is start / end of month
– David Waterworth
Nov 14 '18 at 3:06
Thanks @SanchitKumar, that's useful although I prefer the accepted answer as it gives me the datetime information. The answer you link assumes summer / winter transition is start / end of month
– David Waterworth
Nov 14 '18 at 3:06
add a comment |
2 Answers
2
active
oldest
votes
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')
add a comment |
I'm guessing that Wen's method may be a bit faster, but heres a way of working with the underlying Python datetime
objects with the isdst
attribute from datetime.timetuple
:
>>> is_dst = [x.timetuple().tm_isdst for x in df.index.to_pydatetime()]
>>> pd.Series(is_dst).head()
0 1
1 1
2 1
3 1
4 1
dtype: int64
>>> pd.Series(is_dst).tail()
91 0
92 0
93 0
94 0
95 0
dtype: int64
Example for a single value:
.timetuple()
returns a time.struct_time
;
The tm_isdst flag of the result is set according to the dst() method: tzinfo is None or dst() returns None, tm_isdst is set to -1; else if dst() returns a non-zero value, tm_isdst is set to 1; else tm_isdst is set to 0.
>>> df.index[0].to_pydatetime().timetuple()
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=91, tm_isdst=1)
The constructor will simply check if the date's .dst()
attribute is None, nonzero, or some nonzero value:
def timetuple(self):
"Return local time tuple compatible with time.localtime()."
dst = self.dst()
if dst is None:
dst = -1
elif dst:
dst = 1
else:
dst = 0
It has the method in pandas :-)
– W-B
Nov 14 '18 at 2:19
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292178%2flooking-for-pandas-datetimeindex-is-dst%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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')
add a comment |
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')
add a comment |
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')
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')
edited Nov 14 '18 at 2:24
answered Nov 14 '18 at 2:19
W-BW-B
107k83165
107k83165
add a comment |
add a comment |
I'm guessing that Wen's method may be a bit faster, but heres a way of working with the underlying Python datetime
objects with the isdst
attribute from datetime.timetuple
:
>>> is_dst = [x.timetuple().tm_isdst for x in df.index.to_pydatetime()]
>>> pd.Series(is_dst).head()
0 1
1 1
2 1
3 1
4 1
dtype: int64
>>> pd.Series(is_dst).tail()
91 0
92 0
93 0
94 0
95 0
dtype: int64
Example for a single value:
.timetuple()
returns a time.struct_time
;
The tm_isdst flag of the result is set according to the dst() method: tzinfo is None or dst() returns None, tm_isdst is set to -1; else if dst() returns a non-zero value, tm_isdst is set to 1; else tm_isdst is set to 0.
>>> df.index[0].to_pydatetime().timetuple()
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=91, tm_isdst=1)
The constructor will simply check if the date's .dst()
attribute is None, nonzero, or some nonzero value:
def timetuple(self):
"Return local time tuple compatible with time.localtime()."
dst = self.dst()
if dst is None:
dst = -1
elif dst:
dst = 1
else:
dst = 0
It has the method in pandas :-)
– W-B
Nov 14 '18 at 2:19
add a comment |
I'm guessing that Wen's method may be a bit faster, but heres a way of working with the underlying Python datetime
objects with the isdst
attribute from datetime.timetuple
:
>>> is_dst = [x.timetuple().tm_isdst for x in df.index.to_pydatetime()]
>>> pd.Series(is_dst).head()
0 1
1 1
2 1
3 1
4 1
dtype: int64
>>> pd.Series(is_dst).tail()
91 0
92 0
93 0
94 0
95 0
dtype: int64
Example for a single value:
.timetuple()
returns a time.struct_time
;
The tm_isdst flag of the result is set according to the dst() method: tzinfo is None or dst() returns None, tm_isdst is set to -1; else if dst() returns a non-zero value, tm_isdst is set to 1; else tm_isdst is set to 0.
>>> df.index[0].to_pydatetime().timetuple()
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=91, tm_isdst=1)
The constructor will simply check if the date's .dst()
attribute is None, nonzero, or some nonzero value:
def timetuple(self):
"Return local time tuple compatible with time.localtime()."
dst = self.dst()
if dst is None:
dst = -1
elif dst:
dst = 1
else:
dst = 0
It has the method in pandas :-)
– W-B
Nov 14 '18 at 2:19
add a comment |
I'm guessing that Wen's method may be a bit faster, but heres a way of working with the underlying Python datetime
objects with the isdst
attribute from datetime.timetuple
:
>>> is_dst = [x.timetuple().tm_isdst for x in df.index.to_pydatetime()]
>>> pd.Series(is_dst).head()
0 1
1 1
2 1
3 1
4 1
dtype: int64
>>> pd.Series(is_dst).tail()
91 0
92 0
93 0
94 0
95 0
dtype: int64
Example for a single value:
.timetuple()
returns a time.struct_time
;
The tm_isdst flag of the result is set according to the dst() method: tzinfo is None or dst() returns None, tm_isdst is set to -1; else if dst() returns a non-zero value, tm_isdst is set to 1; else tm_isdst is set to 0.
>>> df.index[0].to_pydatetime().timetuple()
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=91, tm_isdst=1)
The constructor will simply check if the date's .dst()
attribute is None, nonzero, or some nonzero value:
def timetuple(self):
"Return local time tuple compatible with time.localtime()."
dst = self.dst()
if dst is None:
dst = -1
elif dst:
dst = 1
else:
dst = 0
I'm guessing that Wen's method may be a bit faster, but heres a way of working with the underlying Python datetime
objects with the isdst
attribute from datetime.timetuple
:
>>> is_dst = [x.timetuple().tm_isdst for x in df.index.to_pydatetime()]
>>> pd.Series(is_dst).head()
0 1
1 1
2 1
3 1
4 1
dtype: int64
>>> pd.Series(is_dst).tail()
91 0
92 0
93 0
94 0
95 0
dtype: int64
Example for a single value:
.timetuple()
returns a time.struct_time
;
The tm_isdst flag of the result is set according to the dst() method: tzinfo is None or dst() returns None, tm_isdst is set to -1; else if dst() returns a non-zero value, tm_isdst is set to 1; else tm_isdst is set to 0.
>>> df.index[0].to_pydatetime().timetuple()
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=91, tm_isdst=1)
The constructor will simply check if the date's .dst()
attribute is None, nonzero, or some nonzero value:
def timetuple(self):
"Return local time tuple compatible with time.localtime()."
dst = self.dst()
if dst is None:
dst = -1
elif dst:
dst = 1
else:
dst = 0
edited Nov 14 '18 at 3:03
answered Nov 14 '18 at 2:16
Brad SolomonBrad Solomon
13.5k73484
13.5k73484
It has the method in pandas :-)
– W-B
Nov 14 '18 at 2:19
add a comment |
It has the method in pandas :-)
– W-B
Nov 14 '18 at 2:19
It has the method in pandas :-)
– W-B
Nov 14 '18 at 2:19
It has the method in pandas :-)
– W-B
Nov 14 '18 at 2:19
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292178%2flooking-for-pandas-datetimeindex-is-dst%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Maybe this can help your case: stackoverflow.com/questions/44124436/python-datetime-to-season
– Sanchit Kumar
Nov 14 '18 at 2:13
Thanks @SanchitKumar, that's useful although I prefer the accepted answer as it gives me the datetime information. The answer you link assumes summer / winter transition is start / end of month
– David Waterworth
Nov 14 '18 at 3:06