简体   繁体   中英

Trouble resampling pandas timeseries from 1min to 5min data

I have a 1 minute interval intraday stock data which looks like this:

import yfinance as yf
import pandas as pd
n = yf.download('^nsei', period= '5d', interval= '1m')

I am trying to resample it to '5m' data like this:

n = n.resample('5T').agg(dict(zip(n.columns, ['first', 'max', 'min', 'last', 'last', 'sum'])))

But it tries to resample the datetime information which is not in my data. The market data is only available till 03:30 PM, but when I look at the resampled dataframe I find its tried to resample for entire 24 hrs.
How do I stop the resampling till 03:30PM and move on to the succeeding date?
Right now the dataframe has mostly NaN values due to this. Any suggestions will be welcome.

I am not sure what you are trying to achieve with that agg() function. Assuming 'first' refers to the first quantile and 'last' to the last quantile and you want to calculate some statistics per column, I suggest you do the following:

Get your data:

import yfinance as yf
import pandas as pd
n = yf.download('^nsei', period= '5d', interval= '1m')

Resample your data:

resampled_df = n.resample('5T').mean()

Confirm that there are missing values:

print(resampled_df.isna().sum())

Drop missing values (this will remove all rows with missing values, such that resampled rows after 15:30 will disappear):

resampled_df = resampled_df.dropna()

Get the statistics:

statistics = resampled_df.describe()
statistics

Note that describe() will not contain the sum, so in order to add it you could do:

statistics = pd.concat([statistics, resampled_df.agg(['sum'])])
statistics

Output: 在此处输入图像描述

The agg() is to apply individual method of operation for each column, I used this so that I can get to see the 'candlestick' formation as it is called in stock technical analysis.
I was able to fix the issue, by dropping the NaN values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM