Skip to content

Instantly share code, notes, and snippets.

@prithwi
Created January 4, 2016 20:27
Show Gist options
  • 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.
@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