Group together arbitrary date objects that are within a time range of each other

前端 未结 3 1584
执笔经年
执笔经年 2020-12-15 14:31

I want to split the calendar into two-week intervals starting at 2008-May-5, or any arbitrary starting point.

So I start with several date objects:

3条回答
  •  伪装坚强ぢ
    2020-12-15 15:01

    Using a pandas DataFrame with resample works too. Given OP's data, but change "some data here" to 'abcd'.

    >>> import datetime as DT
    >>> raw = ("2010-08-01",
    ...        "2010-06-25",
    ...        "2010-07-01",
    ...        "2010-07-08")
    >>> transactions = [(DT.datetime.strptime(datestring, "%Y-%m-%d"), data) for
    ...                 datestring, data in zip(raw,'abcd')]
    [(datetime.datetime(2010, 8, 1, 0, 0), 'a'),
     (datetime.datetime(2010, 6, 25, 0, 0), 'b'),
     (datetime.datetime(2010, 7, 1, 0, 0), 'c'),
     (datetime.datetime(2010, 7, 8, 0, 0), 'd')]
    

    Now try using pandas. First create a DataFrame, naming the columns and setting the indices to the dates.

    >>> import pandas as pd
    >>> df = pd.DataFrame(transactions,
    ...                   columns=['date','data']).set_index('date')
               data
    date
    2010-08-01    a
    2010-06-25    b
    2010-07-01    c
    2010-07-08    d
    

    Now use the Series Offset Aliases to every 2 weeks starting on Sundays and concatenate the results.

    >>> fortnight = df.resample('2W-SUN').sum()
               data
    date
    2010-06-27    b
    2010-07-11   cd
    2010-07-25    0
    2010-08-08    a
    

    Now drill into the data as needed by weekstart

    >>> fortnight.loc['2010-06-27']['data']
    b
    

    or index

    >>> fortnight.iloc[0]['data']
    b
    

    or indices

    >>> data = fortnight.iloc[:2]['data']
    b
    date
    2010-06-27     b
    2010-07-11    cd
    Freq: 2W-SUN, Name: data, dtype: object
    >>> data[0]
    b
    >>> data[1]
    cd
    

提交回复
热议问题