Search code examples
pythonpython-3.xpandastime-seriesperiod

What are the start and end times of a pandas.Period() object? What role does the parameter 'freq' play in the Period object?


Suppose a Period object is created in Pandas as -

In [1]: pd.Period('2021', 'M')
Out [1]: Period('2021-01', 'M')

What are the start and end points of this period object? How does the frequency M affect those start and end points? In fact, what role does this frequency play here?


Solution

  • The freq specifies the length of the period, is it a daily period or monthly period? etc.. For example if you pass a date 2021-03-21 with a D frequency

    import pandas as pd
    period = pd.Period('2021-03-21', 'D')
    

    then one can know when does the period starts and end by calling the the start_time and end_time attributes

    period.start_time
    >>> Timestamp('2021-03-21 00:00:00')
    period.end_time
    >>> Timestamp('2021-03-21 23:59:59.999999999')
    

    Now, consider the whole month of March, a frequency M is a Monthly period

    period = pd.Period('2021-03-21', 'M')
    period.start_time
    >>> Timestamp('2021-03-01 00:00:00')
    period.end_time
    >>> Timestamp('2021-03-31 23:59:59.999999999')