Optimize reduce_dataframe_footprint function
This commit is contained in:
parent
257c833831
commit
43bdd34964
@ -1355,13 +1355,15 @@ class FreqaiDataKitchen:
|
|||||||
start_mem = df.memory_usage().sum() / 1024**2
|
start_mem = df.memory_usage().sum() / 1024**2
|
||||||
print("Memory usage of dataframe is {:.2f} MB".format(start_mem))
|
print("Memory usage of dataframe is {:.2f} MB".format(start_mem))
|
||||||
|
|
||||||
for col in df.columns[1:]:
|
df_dtypes = df.dtypes
|
||||||
col_type = df[col].dtype
|
for column, dtype in df_dtypes.items():
|
||||||
if col_type != object:
|
if column in ['open', 'high', 'low', 'close', 'volume']:
|
||||||
if str(col_type)[:3] == "int":
|
continue
|
||||||
df[col] = df[col].astype(np.int32)
|
if dtype == np.float64:
|
||||||
else:
|
df_dtypes[column] = np.float32
|
||||||
df[col] = df[col].astype(np.float32)
|
elif dtype == np.int64:
|
||||||
|
df_dtypes[column] = np.int32
|
||||||
|
df = df.astype(df_dtypes)
|
||||||
|
|
||||||
end_mem = df.memory_usage().sum() / 1024**2
|
end_mem = df.memory_usage().sum() / 1024**2
|
||||||
print("Memory usage after optimization is: {:.2f} MB".format(end_mem))
|
print("Memory usage after optimization is: {:.2f} MB".format(end_mem))
|
||||||
|
Loading…
Reference in New Issue
Block a user