looking for Pandas.DateTimeIndex.is_dst()












3















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.










share|improve this question

























  • 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
















3















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.










share|improve this question

























  • 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














3












3








3








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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












2 Answers
2






active

oldest

votes


















4














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')





share|improve this answer

































    2














    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





    share|improve this answer


























    • It has the method in pandas :-)

      – W-B
      Nov 14 '18 at 2:19











    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    4














    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')





    share|improve this answer






























      4














      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')





      share|improve this answer




























        4












        4








        4







        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')





        share|improve this answer















        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')






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '18 at 2:24

























        answered Nov 14 '18 at 2:19









        W-BW-B

        107k83165




        107k83165

























            2














            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





            share|improve this answer


























            • It has the method in pandas :-)

              – W-B
              Nov 14 '18 at 2:19
















            2














            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





            share|improve this answer


























            • It has the method in pandas :-)

              – W-B
              Nov 14 '18 at 2:19














            2












            2








            2







            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





            share|improve this answer















            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






            share|improve this answer














            share|improve this answer



            share|improve this answer








            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



















            • 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


















            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python