Skip to content

BUG: Slower DataFrame.plot with DatetimeIndex #61398

Open
@Abdelgha-4

Description

@Abdelgha-4

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

# Imports & data generation
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

num_rows = 500
num_cols = 2000

index = pd.date_range(start="2020-01-01", periods=num_rows, freq="D")
test_df = pd.DataFrame(np.random.randn(num_rows, num_cols).cumsum(axis=0), index=index)


# Very Slow plot (1m 11.6s)
test_df.plot(legend=False, figsize=(12, 8))
plt.show()


# Much faster Plot using this workaround: (6.1s)

# 1. Plot a single column with dates to copy the right ticks
ax1 = test_df.iloc[:, 0].plot(figsize=(12, 6), legend=False)
xticks = ax1.get_xticks()
xticklabels = [label.get_text() for label in ax1.get_xticklabels()]
plt.close(ax1.figure)

# 2. Faster plot with no date index
ax2 = test_df.reset_index(drop=True).plot(legend=False, figsize=(12, 8))
# 3. Inject the date X axis info
num_ticks = len(xticks)
new_xticks = np.linspace(0, num_rows - 1, num_ticks)
ax2.set_xlim(0, num_rows - 1)
ax2.set_xticks(new_xticks)
ax2.set_xticklabels(xticklabels)
plt.show()

Issue Description

Plotting a large DataFrame with a DatetimeIndex and many rows and columns results in extremely slow rendering times. This issue can be surprisingly mitigated by first plotting a single column to generate the correct ticks and labels, then resetting the index and copying the ticks to plot the full DataFrame, gaining +11x speed improvement. This may suggests that a similar logic may be applied (if found consistent) to improve speed when applied.

Expected Behavior

No big difference in ploting time depending on the index type, especially if avoidable with the trick above.

Installed Versions

INSTALLED VERSIONS

commit : d9cdd2e
python : 3.12.4.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 9, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : fr_FR.cp1252

pandas : 2.2.2
numpy : 2.0.1
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : 75.3.0
pip : 25.0.1
Cython : None
pytest : 8.3.3
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 5.3.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.4
IPython : 8.26.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.9.2
numba : None
numexpr : 2.10.1
odfpy : None
openpyxl : 3.1.5
pandas_gbq : None
pyarrow : 17.0.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.14.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

Labels

DatetimeDatetime data dtypePerformanceMemory or execution speed performanceVisualizationplotting

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions