In general, if you have a data frame in the form you specified, you need to follow these steps:
- put a
Date in the pointer resample index.
You have a case of applying different functions to different columns. See
You can change the selection in various ways. For example, you can take the average or count or so on. check re-fetch pandas .
You can also apply custom aggregators (check the same link). With this in mind, the code snippet for your case can be represented as:
f['Date'] = pd.to_datetime(f['Date']) f.set_index('Date', inplace=True) f.sort_index(inplace=True) def take_first(array_like): return array_like[0] def take_last(array_like): return array_like[-1] output = f.resample('W',
Here, W stands for weekly resampling, which by default covers the period from Monday to Sunday. To label loffset Mondays, use loffset . There are several predefined day qualifiers. Look at the pandas offsets . You can even define custom offsets ( see ).
Returning to the recount method . Here for Open and Close you can specify custom methods to get the first value, etc. And passing the function handle to the how argument.
This answer is based on the assumption that the data seems to be daily, that is, for each day you have only 1 record. Also there is no data for non-working days. those. Sat and Sun Thus, if you take the last data point for the week, then on Friday everything is in order. If you want, you can use the work week instead of "W". In addition, for more complex data, you can use groupby to group weekly data, and then work with time indices inside them.
By the way, the essence of the solution can be found at: https://gist.github.com/prithwi/339f87bf9c3c37bb3188.