Description
Pandas version checks
-
I have checked that this issue has not already been reported.
-
I have confirmed this issue exists on the latest version of pandas.
-
I have confirmed this issue exists on the main branch of pandas.
Reproducible Example
I want to transform pandas DataFrames to dictionaries in order to hand over data as python std lib object. I had the idea of using the to_dict method. My prior experience with pandas and numpy was that my custom few liners never can compete with the provided methods performance wise. But when I noticed that the to_dict method can become a bottle neck I tried out a rather simple attempt to achieve what I wanted. I measured something like a improvement of factor 5, which is very much noticeable to me, as the transformation with to_dict can take tens of seconds for me.
I wonder if this can be improved or whether my method has some draw backs I can't anticipate right now.
import pandas as pd
import time
data_frame = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn'
'-data/master/iris.csv')
t0 = time.perf_counter_ns()
dict_to_dict = data_frame.to_dict('list')
t1 = time.perf_counter_ns()
values = data_frame.values
dict_custom = {}
for i, col in enumerate(data_frame.columns):
dict_custom[col] = values[:,i].tolist()
t2 = time.perf_counter_ns()
print(f"Time with to_dict('list'):\t{(t1-t0)/10**9},\n"
f"Time with custom method:\t{(t2-t1)/10**9}"
)
Installed Versions
INSTALLED VERSIONS
commit : 2e218d1
python : 3.8.7.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.17763
machine : AMD64
processor : Intel64 Family 6 Model 85 Stepping 4, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252
pandas : 1.5.3
numpy : 1.24.1
pytz : 2022.7.1
dateutil : 2.8.2
setuptools : 65.6.3
pip : 22.3.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None
Prior Performance
No response