Skip to content

Instantly share code, notes, and snippets.

@prithwi
Created January 4, 2016 20:27
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save prithwi/339f87bf9c3c37bb3188 to your computer and use it in GitHub Desktop.
Save prithwi/339f87bf9c3c37bb3188 to your computer and use it in GitHub Desktop.
daily stock to weekly stock values
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@kier0n
Copy link

kier0n commented Oct 25, 2017

Beautiful !! thanks.

@aristid73
Copy link

this is fantastic and has helped me move along, thanks.
It gives a warning:

Warning (from warnings module):line 79
loffset=pandas.offsets.timedelta(days=-6)) # to put the labels to Monday
FutureWarning: how in .resample() is deprecated
the new syntax is .resample(...)..apply()

I am not skilled enough to rewrite it into the new syntax, anyone up to the task?
thanks in advance!

@underdpt
Copy link

I can't comment on original stackoverflow post but I have two questions for this:

  1. Why using custom handlers for taking the first and last value, when pandas has 'first' and 'last' aggregators
  2. What's the purpose of the last line of code
    output = output[['Open', 'High', 'Low', 'Close', 'Volume']]
    Isn't output the same before and after this line? This is only for ordering the columns?

@underdpt
Copy link

@aristid73, you can use agg() function, simply:

output = df.resample('W')                                 # Weekly resample
                    .agg({'Open': take_first, 
                          'High': 'max',
                          'Low': 'min',
                          'Close': take_last,
                          'Volume': 'sum'}) 
output.index = output.index + pd.DateOffset(days=-6  # to put the labels to Monday

@arbelkf
Copy link

arbelkf commented Mar 29, 2018

Thanks all of you:
the final code that worked for me after reading all of those comments and some other:

`import pandas as pd
import pandas_datareader as web
import datetime

ticker = "MMM"
start = datetime.datetime(2018, 2, 5)
end = datetime.datetime(2018, 2, 25)

f = web.DataReader(ticker, 'yahoo', start, end)
f.index = pd.to_datetime(f.index)

def take_first(array_like):
return array_like[0]

def take_last(array_like):
return array_like[-1]
output = f.resample('W').agg({'Open': take_first,
'High': 'max',
'Low': 'min',
'Close': take_last,
'Adj Close': take_last,
'Volume': 'sum'}) # to put the labels to Monday

output = output[['Open', 'High', 'Low', 'Close','Adj Close', 'Volume']]
output.index = output.index + pd.DateOffset(days=-6)
print (output)`

@gswaroop-g1
Copy link

Now (pandas 1.0.5) we don't even need to define the take_first and take_last functions. 'first' and 'last' would work fine.

output = df.resample('W')                                 # Weekly resample
                    .agg({'Open': 'first', 
                          'High': 'max',
                          'Low': 'min',
                          'Close': 'last',
                          'Volume': 'sum'}) 
output.index = output.index + pd.DateOffset(days=-6)  # to put the labels to Monday

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment