Change dates of default "quarter" in pandas
Question:
Is there any way of changing the "default start and end dates", of what is interpreted as the "quarters" in Pandas / Numpy? I’ve looked through the docs and tried reading the pandas source-code, but with no results to show for it.
import pandas as pd
date_range = pd.date_range(start='2020-01-01', end='2021-01-01')
df_dates = pd.DataFrame({'dates' : date_range}, index = date_range)
print(df_dates['dates'].dt.quarter.drop_duplicates())
Results in these "start dates" of the quarters.
2020-01-01 1
2020-04-01 2
2020-07-01 3
2020-10-01 4
Name: dates, dtype: int64
So I want to change these to different dates, if possible. As a "saved offset" that lives in the "current session"…
Desired output would be something like this, on the dt.quarter-accessor (if pushed one month ahead.)
2020-02-01 1
2020-05-01 2
2020-08-01 3
2020-11-01 4
Name: dates, dtype: int64
Answers:
Do you mean by?
print(df_dates['dates'].dt.quarter.drop_duplicates().shift(1, freq='MS'))
Output:
2020-02-01 1
2020-05-01 2
2020-08-01 3
2020-11-01 4
Name: dates, dtype: int64
Is there any way of changing the "default start and end dates", of what is interpreted as the "quarters" in Pandas / Numpy? I’ve looked through the docs and tried reading the pandas source-code, but with no results to show for it.
import pandas as pd
date_range = pd.date_range(start='2020-01-01', end='2021-01-01')
df_dates = pd.DataFrame({'dates' : date_range}, index = date_range)
print(df_dates['dates'].dt.quarter.drop_duplicates())
Results in these "start dates" of the quarters.
2020-01-01 1
2020-04-01 2
2020-07-01 3
2020-10-01 4
Name: dates, dtype: int64
So I want to change these to different dates, if possible. As a "saved offset" that lives in the "current session"…
Desired output would be something like this, on the dt.quarter-accessor (if pushed one month ahead.)
2020-02-01 1
2020-05-01 2
2020-08-01 3
2020-11-01 4
Name: dates, dtype: int64
Do you mean by?
print(df_dates['dates'].dt.quarter.drop_duplicates().shift(1, freq='MS'))
Output:
2020-02-01 1
2020-05-01 2
2020-08-01 3
2020-11-01 4
Name: dates, dtype: int64