Skip to content

Commit 5d37c12

Browse files
committed
CME_Rollup.py - Updated
Working Rollup code with changes made with the forecasted_dt column
1 parent ee336fc commit 5d37c12

1 file changed

Lines changed: 16 additions & 14 deletions

File tree

CME_Rollup.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,24 @@
1717
data_directory = os.getcwd()
1818

1919
if __name__ == '__main__':
20-
#cme_futures_obj = StationForecastDataset("cme_futures-daily", ipfs_timeout=ipfs_timeout)
2120
cme_futures_obj = StationForecastDataset("cme_futures-daily", ipfs_timeout=None)
2221
current_head = cme_futures_obj.head
2322
current_metadata = cme_futures_obj.get_metadata(current_head)
2423
station_data_dictionary = {} # Key will be the station, and the value will be a pandas dataframe with the data
2524
dataframes = {}
26-
while True: #current_metadata['previous hash'] is not None:
27-
#current_date = current_metadata["date range"][0]
28-
#current_datetime = datetime.date(int(current_date[0:4]), int(current_date[5:7]), int(current_date[8:])) #Beware current date's type changes here
25+
while True:
2926
current_datetime = datetime.datetime.strptime(current_metadata["date range"][0], "%Y-%m-%d").date()
3027
station_features = json.loads(cme_futures_obj.get_stations(current_datetime))['features']
3128
for feature in station_features:
32-
station_name = feature["properties"]["station name"] #this is the station name
33-
29+
station_name = feature["properties"]["station name"] #this is the station name
3430
csv_text = cme_futures_obj.get_data(station_name, (current_datetime))
3531
df = pd.read_csv(StringIO(csv_text))
36-
df['forecasted_dt'] = current_datetime.strftime("%Y-%m")
32+
try:
33+
df.rename(columns={"DATE": "forecasted_dt", "value": "SETT"}, inplace=True)
34+
df["dt"] = current_datetime.strftime("%Y-%m-%d")
35+
except KeyError:
36+
print("Required columns not found in the dataset. Please verify the column names.")
37+
continue
3738
if station_name not in station_data_dictionary:
3839
station_data_dictionary[station_name] = {
3940
"hashes": [current_head],
@@ -59,31 +60,32 @@
5960
data["previous_hashes"] = data["hashes"][1:]
6061
data["hashes"] = data["hashes"][:-1]
6162

62-
# Print the station data dictionary in the desired format
63+
6364
for station_key, data in station_data_dictionary.items():
64-
station_filename = f"{station_key}_table.csv" # Generate a filename based on the station name
65+
station_filename = f"{station_key}.csv" # Generate a filename based on the station name
6566
with open(station_filename, 'w', newline='') as file:
6667
writer = csv.writer(file)
6768
writer.writerow(["forecasted_dt", "SETT", "dt"])
6869
for hash_value, previous_hash, forecast_date in zip(data["hashes"], data["previous_hashes"], data["forecast_dates"]):
6970
df = dataframes[station_key][hash_value]
7071
for _, row in df.iterrows():
7172
forecasted_dt = str(row['forecasted_dt'])
73+
forecasted_dt = forecasted_dt[:-3]
7274
sett = str(row['SETT'])
7375
dt = str(forecast_date)
7476
writer.writerow([forecasted_dt, sett, dt])
7577
station_data = {}
7678
for file_station_key, data in station_data_dictionary.items():
77-
station_filename = f"{file_station_key}_table.csv" # Generate the filename of the CSV file
79+
station_filename = f"{file_station_key}.csv" # Generate the filename of the CSV file
7880
file_path = os.path.join(data_directory, station_filename) # Create the full file path
7981
station_data[file_station_key] = pd.read_csv(file_path)
8082

81-
file_station_name = "D2X"
83+
"""file_station_name = "D2X"
8284
station_df = station_data[file_station_name]
8385
station_df['dt'] = pd.to_datetime(station_df['dt']).dt.strftime("%Y-%m-%d")
84-
print(station_df)
86+
print(station_df)"""
87+
import ipdb;ipdb.set_trace()
8588

8689

87-
import ipdb;ipdb.set_trace()
90+
8891

89-

0 commit comments

Comments
 (0)