0

Let's generate a df with random numbers using code below.

test = pd.DataFrame(index=pd.date_range('2019-03-28', '2020-07-31'))
np.random.seed(101)
test['Value'] = np.random.rand(test.shape[0])*1000

If I resample on quarterly basis and get the last value the code to do that would be as follows:

test.resample('Q').last()

How can I get last value on semi-annual resample and financial year resample?

Expected Output - Semi-Annual

+------------+-------------+
|    Date    |    Value    |
+------------+-------------+
| 30/06/2019 | 13.24101109 |
| 31/12/2019 | 67.13832944 |
| 30/06/2020 | 757.1984631 |
+------------+-------------+

Expected Output - Financial Year

+------------+-------------+
|    Date    |    Value    |
+------------+-------------+
| 30/06/2019 | 13.24101109 |
| 30/06/2020 | 757.1984631 |
+------------+-------------+
Lopez
  • 461
  • 5
  • 19

1 Answers1

0

Semi-annual:

test['2019-06-30':].asfreq('12M')

            Value
2019-06-30  13.241011
2019-12-31  67.138329
2020-06-30  757.198463

Annual:

test['2019-06-30':].asfreq(freq='12M')

            Value
2019-06-30  13.241011
2020-06-30  757.198463
Ted
  • 1,189
  • 8
  • 15