python - Find end date of quarter given date, pandas -


assume have table like

table = [[datetime.datetime(2015, 1, 1), 1, 0.5],          [datetime.datetime(2015, 1, 27), 1, 0.5],          [datetime.datetime(2015, 1, 31), 1, 0.5],          [datetime.datetime(2015, 2, 1), 1, 2],          [datetime.datetime(2015, 2, 3), 1, 2],          [datetime.datetime(2015, 2, 15), 1, 2],          [datetime.datetime(2015, 2, 28), 1, 2],          [datetime.datetime(2015, 3, 1), 1, 3],          [datetime.datetime(2015, 3, 17), 1, 3],          [datetime.datetime(2015, 3, 31), 1, 3]]  df = pd.dataframe(table, columns=['date', 'id', 'value']) 

is there way specific end date of actual quarter given dates in column date? e.g., add column q_date df such that

        date  id  value  qdate 0 2015-01-01   1    0.5  2015-03-31 1 2015-01-27   1    0.5  2015-03-31 2 2015-01-31   1    0.5  2015-03-31 3 2015-02-01   1    2.0  2015-03-31 4 2015-02-03   1    2.0  2015-03-31 5 2015-02-15   1    2.0  2015-03-31 6 2015-02-28   1    2.0  2015-03-31 7 2015-03-01   1    3.0  2015-03-31 8 2015-03-17   1    3.0  2015-03-31 9 2015-03-31   1    3.0  2015-03-31 

i've considered first quarter simplicity - know date is.

you can use pd.tseries.offsets.quaterend() achieve goal here.

import pandas pd import datetime  # data # ================================ table = [[datetime.datetime(2015, 1, 1), 1, 0.5],          [datetime.datetime(2015, 1, 27), 1, 0.5],          [datetime.datetime(2015, 1, 31), 1, 0.5],          [datetime.datetime(2015, 2, 1), 1, 2],          [datetime.datetime(2015, 2, 3), 1, 2],          [datetime.datetime(2015, 2, 15), 1, 2],          [datetime.datetime(2015, 2, 28), 1, 2],          [datetime.datetime(2015, 3, 1), 1, 3],          [datetime.datetime(2015, 3, 17), 1, 3],          [datetime.datetime(2015, 3, 31), 1, 3]]  df = pd.dataframe(table, columns=['date', 'id', 'value'])  # processing # ================================ # in case of 2015.03.31, simple quarterend roll forward next quarter, use dateoffset here make robust df['qdate'] = [date - pd.tseries.offsets.dateoffset(days=1) + pd.tseries.offsets.quarterend() date in  df.date]  print(df)           date  id  value      qdate 0 2015-01-01   1    0.5 2015-03-31 1 2015-01-27   1    0.5 2015-03-31 2 2015-01-31   1    0.5 2015-03-31 3 2015-02-01   1    2.0 2015-03-31 4 2015-02-03   1    2.0 2015-03-31 5 2015-02-15   1    2.0 2015-03-31 6 2015-02-28   1    2.0 2015-03-31 7 2015-03-01   1    3.0 2015-03-31 8 2015-03-17   1    3.0 2015-03-31 9 2015-03-31   1    3.0 2015-03-31 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -