how to understand closed and label arguments in pandas resample method?

前端 未结 3 1304
灰色年华
灰色年华 2020-12-01 17:22

Based on the pandas documentation from here: Docs

And the examples:

>>> index = pd.date_range(\'1/1/2000\', periods=9, freq=\'T\')
>>&         


        
3条回答
  •  无人及你
    2020-12-01 17:48

    It is important that resampling is performed by first producing a raster which is a sequence of instants (not periods, intervals, durations), and it is done independent of the 'label' and 'closed' parameters. It uses only the 'freq' parameter and 'loffset'. In your case, the system will produce the following raster:

    2000-01-01 00:00:00
    2000-01-01 00:03:00
    2000-01-01 00:06:00
    2000-01-01 00:09:00
    

    Note again that at this moment there is no interpretation in terms of intervals or periods. You can shift it using 'loffset'.

    Then the system will use the 'closed' parameter in ordre to choose among two options:

    • (start, end]

    • [start, end)

    Here start and end are two adjacent time stamps in the raster. The 'label' parameter is used to choose whether start or end are used as a representative of the interval.

    In your example, if you choose closed='right' then you will get the following intervals:

    ( previous_interval , 2000-01-01 00:00:00] - {0}
    (2000-01-01 00:00:00, 2000-01-01 00:03:00] - {1,2,3}
    (2000-01-01 00:03:00, 2000-01-01 00:06:00] - {1,2,3}
    (2000-01-01 00:06:00, 2000-01-01 00:09:00] - {4,5,6}
    (2000-01-01 00:09:00, next_interval ] - {7,8}
    

    Note that after you aggregate the values over these intervals, the result is displayed in two versions depending on the 'label' parameter, that is, whether one and the same interval is represented by its left or right time stamp.

提交回复
热议问题