Resample Daily OHLCV to weekly OHLCV

I would like to redo / convert Daily (ohlcv) to Weekly (ohlcv). Is it possible to do this with pandas?

The sample data is as follows (1 week of daily data) in Dictonar format:

   {'High': {<Timestamp: 2007-03-02 00:00:00>: 1384.5,
  <Timestamp: 2007-03-05 00:00:00>: 1373.0,
  <Timestamp: 2007-03-06 00:00:00>: 1378.75,
  <Timestamp: 2007-03-07 00:00:00>: 1381.75,
  <Timestamp: 2007-03-08 00:00:00>: 1388.75},
 'Last': {<Timestamp: 2007-03-02 00:00:00>: 1365.0,
  <Timestamp: 2007-03-05 00:00:00>: 1351.5,
  <Timestamp: 2007-03-06 00:00:00>: 1374.5,
  <Timestamp: 2007-03-07 00:00:00>: 1372.0,
  <Timestamp: 2007-03-08 00:00:00>: 1384.5},
 'Low': {<Timestamp: 2007-03-02 00:00:00>: 1364.25,
  <Timestamp: 2007-03-05 00:00:00>: 1350.5,
  <Timestamp: 2007-03-06 00:00:00>: 1362.0,
  <Timestamp: 2007-03-07 00:00:00>: 1370.75,
  <Timestamp: 2007-03-08 00:00:00>: 1369.25},
 'Open': {<Timestamp: 2007-03-02 00:00:00>: 1378.5,
  <Timestamp: 2007-03-05 00:00:00>: 1356.75,
  <Timestamp: 2007-03-06 00:00:00>: 1365.25,
  <Timestamp: 2007-03-07 00:00:00>: 1374.0,
  <Timestamp: 2007-03-08 00:00:00>: 1370.0},
 'Volume': {<Timestamp: 2007-03-02 00:00:00>: 1706906,
  <Timestamp: 2007-03-05 00:00:00>: 1984041,
  <Timestamp: 2007-03-06 00:00:00>: 1397911,
  <Timestamp: 2007-03-07 00:00:00>: 1255484,
  <Timestamp: 2007-03-08 00:00:00>: 798237}}

      

Thanks and regards.

+3


source to share


2 answers


Once you have the data in the DataFrame, you can do this:

 ohlc_dict = {
    'Open':'first',
    'High':'max',
    'Low':'min',
    'Close':'last',
    'Volume':'sum'
    }

DataFrame.resample('W-Fri', how=ohlc_dict)

      



This will give you the ohlc data for the week ending Friday.

+7


source


An example given in the official documentation:

# Weekly means
In [1305]: ts.resample('W', how='mean')
Out[1305]: 
2011-01-01   -0.319569
2011-01-02   -0.337703
2011-01-03    0.117258
Freq: W

      



You can offset dates on a specific day of the week, for example, 'W-SUN'

or 'W-MON'

.

Link to documentation

+2


source







All Articles