Pandas version checks

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

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

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

Reproducible Example

import pandas as pd
import xarray as xr
import numpy as np
import time

# Minimal reproducible example: Pandas Series print performance with large xarray DataArrays
print("Pandas Series Print Performance Issue")
print("=" * 40)

# Create minimal test data
np.random.seed(42)
time_coords = pd.date_range('2023-01-01', periods=2, freq='D')
x_coords = np.linspace(0, 10, 256)
y_coords = np.linspace(0, 10, 256)

# Create one large xarray DataArray
data = np.random.randn(2, 256, 256)
large_dataarray = xr.DataArray(
    data,
    coords={'time': time_coords, 'y': y_coords, 'x': x_coords},
    dims=['time', 'y', 'x']
)

# Create minimal pandas Series with large DataArray
series = pd.Series({
    'id': 1,
    'data': large_dataarray,
    'name': 'test_series'
})

print(f"DataArray size: {large_dataarray.nbytes / 1024 / 1024:.1f} MB")
print(f"DataArray shape: {large_dataarray.shape}")

# Method 1: Print Series directly
print("\nMethod 1: Print Series")
start_time = time.time()
print(series)
method1_time = time.time() - start_time

# Method 2: Extract DataArray first, then print it
print("\nMethod 2: Extract DataArray first, then print")
start_time = time.time()
extracted_da = series['data']
print(extracted_da)
method2_time = time.time() - start_time

# Results
print(f"\nTiming Results:")
print(f"Method 1 (print Series): {method1_time:.4f} seconds")
print(f"Method 2 (extract + print DataArray): {method2_time:.4f} seconds")
print(f"Difference: {abs(method1_time - method2_time):.4f} seconds")

if method1_time > method2_time:
    ratio = method1_time / method2_time
    print(f"Method 1 is {ratio:.1f}x slower than Method 2")
else:
    ratio = method2_time / method1_time
    print(f"Method 2 is {ratio:.1f}x slower than Method 1")

# Environment info
print(f"\nEnvironment:")
print(f"Pandas: {pd.__version__}")
print(f"XArray: {xr.__version__}")
print(f"NumPy: {np.__version__}")

Issue Description

Hi Pandas team, so I was working with pandas series and was trying to put an xarray into a cell.

So when I was trying to print out the pandas series with the xarray, I found that it is extremely slow, directly printing out the pandas series is 1000X slower than getting the xarray and then print out the xarray.

The above script is an example with an xarray inside a pandas series, and the time comparison between printing the pandas series directly and get the xarray first and them print the values.

Possible issue: String formatting with xarray.

Image

Expected Behavior

Similar time consumption for directly printing the pandas series and get the xarray and print the content.

Installed Versions

INSTALLED VERSIONS ------------------ commit : c888af6d0bb674932007623c0867e1fbd4bdc2c6 python : 3.11.7 python-bits : 64 OS : Linux OS-release : 6.11.0-29-generic Version : #29~24.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Jun 26 14:16:59 UTC 2 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : en_AU.UTF-8 LOCALE : en_AU.UTF-8 pandas : 2.3.1 numpy : 2.3.1 pytz : 2025.2 dateutil : 2.9.0.post0 pip : 23.2.1 Cython : None sphinx : None IPython : None adbc-driver-postgresql: None adbc-driver-sqlite : None bs4 : None blosc : None bottleneck : None dataframe-api-compat : None fastparquet : None fsspec : None html5lib : None hypothesis : None gcsfs : None jinja2 : None lxml.etree : None matplotlib : 3.10.3 numba : None numexpr : None odfpy : None openpyxl : None pandas_gbq : None psycopg2 : None pymysql : None pyarrow : None pyreadstat : None pytest : None python-calamine : None pyxlsb : None s3fs : None scipy : 1.16.0 sqlalchemy : None tables : None tabulate : None xarray : 2025.7.0 xlrd : None xlsxwriter : None zstandard : None tzdata : 2025.2 qtpy : None pyqt5 : None None

Comment From: arthurlw

From a surface-level investigation, it seems the difference is caused because when printing a Series, pandas forces repr to be called on each element, whereas I assume extracting the data first requires calling repr only once.

Whether this is something that should be changed needs more discussion.

Thanks for opening this issue!