I get a FileNotFoundError: [Errno 2] No such file or directory: 'PriceData.pick'
This error is encountered in Reinforcement Learning Part II. It is found under Resample Price Data.
It is listed under, Resample the 5 mins bars to 1 hour bars using the resample() and agg() method of pandas. Syntax: df = DataFrame.resample(frequency, label, closed). df.agg(func).
The code is:
# Import necessary libraries
import pandas as pd
from datetime import datetime, timedelta
import datetime
# The data is stored in the directory 'data_modules'
# path = '../data_modules/'
# Read the 5 mins price data
bars5m = pd.read_pickle('PriceData.pick') <- error
ohlcv_dict = {
'open': 'first',
'high': 'max',
'low': 'min',
'close': 'last',
'volume': 'sum'
}
# Resample 5 mins data to 1 hour
freq = bars5m.resample('1H', label='right', closed='right')
bars1h = freq.agg(ohlcv_dict)
bars1h = bars1h.dropna()
print(bars1h.head())
Please advise,
William P
Hi William,
The error might be due an incorrect file name or path. I would suggest you to check the file name, its extension and the path where the file is located on the system.
Hope this helps!
Thanks,
Akshay
It is not the path because all files are in the directory where all *.pipynb are stored. All information that I sent was directly from the class notes. The information from the class notes are from Reinforcement Learning Part II.
Here are the class notes that was done to resolve the 'PriceData.pick' problem.
Read Price data
The OHLCV data is stored in the compressed pickle file. This is the 5 minutes data starting from 2010-04-01 to 2020-08-31. You can download this data from the last section of this course 'Python Codes and Data' unit.
To read a pickle file, you can use read_pickle method of pandas. The syntax is shown below.
Syntax: import pandas as pd
pd.read_pickle(filename)
filename: name of the file in the string format. The extension of the compressed file is bz2.
In [1]: # Import pickle
import pandas as pd
# Import datetime
from datetime import datetime, timedelta
import datetime
# Read the pickle file
bars5m = pd.read_pickle('PriceData5m.bz2')
bars5m.head()
Out [1]: Time open high low close volume
2010-01-04 09:35:00-05:00 91.711 91.809 91.703 91.760 4448908.0
2010-01-04 09:40:00-05:00 91.752 91.973 91.752 91.932 4380988.0
Resample price data
We use resample() and agg() method for the resampling of time series.
Syntax: DataFrame.resample(frequency, label, closed). agg(func)
Parameters:
frequency: values are '1D' & '1H'
label: Label parameter takes 'left', 'right' as an input.
closed: Closed parameter takes 'left', 'right' as an input.
func: Function to use for aggregating the data. For dataframe, can pass a dictionary, if keys are dataframe column names.
Returns: DataFrame with resampled time series
Create a dictionary to map the open, high, low, close, volume open is the 'first' value in the set of defined frequency
high is the 'max' value in the set of defined frequency low is the min value in the set of defined frequency close is the
'last' value in the set of defined frequency volume is the 'sum' of volume in the set of defined frequency.
ohlcv_dict = { 'open': 'first', 'high': 'max', 'low': 'min', 'close': 'last', 'volume': 'sum' }
bars1h = bars5m.resample('1H', label='right', closed='right').agg(ohlcv_dict).dropna()
bars1h.head()
bars1d = bars5m.resample('1D', label='right', closed='right').agg(ohlcv_dict).dropna()
bars1d.head()
Initialize Game Class
The whole trading game is played inside the Game class. In this notebook you learned to initialize the Game class. In the later sections, you will learn to assemble the state, update the trading position and calculate reward in the individual notebooks and finally fit all these inside the Game class to complete that.
Read Price Data Section 8, Unit 3
Read a compressed pickle file which contains the 5 mins price data. Store it in a dataframe which will be used for our analysis later.
Syntax: pandas.read_pickle(filename)
Parameters: filename: name of the file in the string format. The extension of the compressed file is bz2.
Returns: DataFrame
Instructions We have stored the location of the pickle file in a variable called path which has to be passed as an argument.
Import pickle
import pandas as pd
Read the pickle file
bars5m = pd.read_pickle('PriceData5m.bz2')
print(bars5m.head())
open high low close &nb
Hi William,
We have replied to this over email. Hope the solution worked. Please feel free to reach out if you are still facing any issues.
Thanks,
Akshay