import time
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
start = time.time()
df.to_csv('df.csv')
csv_time = time.time() - start
start = time.time()
df.to_feather('df.feather')
feather_time = time.time() - start
start = time.time()
df.to_pickle('df.pickle')
pickle_time = time.time() - start
start = time.time()
df.to_hdf('df.h5', 'df')
hdf5_time = time.time() - start
start = time.time()
df.to_parquet('df.parquet')
parquet_time = time.time() - start
start = time.time()
pd.read_csv('df.csv')
csv_read_time = time.time() - start
start = time.time()
pd.read_feather('df.feather')
feather_read_time = time.time() - start
start = time.time()
pd.read_pickle('df.pickle')
pickle_read_time = time.time() - start
start = time.time()
pd.read_hdf('df.h5', 'df')
hdf5_read_time = time.time() - start
start = time.time()
pd.read_parquet('df.parquet')
parquet_read_time = time.time() - start
print('Writing:')
print(f' Time to write CSV: {csv_time:.5f} seconds')
print(f' Time to write feather: {feather_time:.5f} seconds')
print(f' Time to write pickle: {pickle_time:.5f} seconds')
print(f' Time to write HDF5: {hdf5_time:.5f} seconds')
print(f' Time to write parquet: {parquet_time:.5f} seconds')
writing_times = {
'CSV': csv_time,
'feather': feather_time,
'pickle': pickle_time,
'HDF5': hdf5_time,
'parquet': parquet_time
}
fastest_format_writing = min(writing_times, key=writing_times.get)
slowest_format_writing = max(writing_times, key=writing_times.get)
print(f'The fastest file format for writing is {fastest_format_writing}')
print(f'The slowest file format for writing is {slowest_format_writing}')
reading_times = {
'CSV': csv_read_time,
'feather': feather_read_time,
'pickle': pickle_read_time,
'HDF5': hdf5_read_time,
'parquet': parquet_read_time
}
fastest_format_reading = min(reading_times, key=reading_times.get)
slowest_format_reading = max(reading_times, key=reading_times.get)
print(f'The fastest file format for reading is {fastest_format_reading}')
print(f'The slowest file format for reading is {slowest_format_reading}')