-
-
Notifications
You must be signed in to change notification settings - Fork 19.2k
Description
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
import datetime
import pandas as pd
values = [
True,
1,
1.2,
"Hello",
[1, "b"],
{"key": "value"},
datetime.datetime(2000, 1, 1),
pd.Timestamp(2000, 1, 1),
pd.offsets.MonthEnd(),
pd.Timedelta(days=1),
pd.Period("2000-01-01"),
]
for value in values:
series = pd.Series(value)
frame = pd.DataFrame({"index": [], "columns": [], "values": []})
frame = frame.astype({"values": series.dtype})
print(f"Testing with {type(value)} ({frame['values'].dtype} dtype)...", end="")
try:
frame.pivot(index="index", columns="columns", values="values")
print("works.")
except Exception:
print("fails!")
raise
Issue Description
Pivoting an empty DataFrame
fails if the column used for the values has a dtype derived from a Period
. the above code results in the following output:
Testing with <class 'bool'> (bool dtype)...works.
Testing with <class 'int'> (int64 dtype)...works.
Testing with <class 'float'> (float64 dtype)...works.
Testing with <class 'str'> (object dtype)...works.
Testing with <class 'list'> (object dtype)...works.
Testing with <class 'dict'> (object dtype)...works.
Testing with <class 'datetime.datetime'> (datetime64[ns] dtype)...works.
Testing with <class 'pandas._libs.tslibs.timestamps.Timestamp'> (datetime64[ns] dtype)...works.
Testing with <class 'pandas._libs.tslibs.offsets.MonthEnd'> (object dtype)...works.
Testing with <class 'pandas._libs.tslibs.timedeltas.Timedelta'> (timedelta64[ns] dtype)...works.
Testing with <class 'pandas._libs.tslibs.period.Period'> (period[D] dtype)...fails!
Traceback (most recent call last):
File "/home/ken/no_backup/pivot_example/pivot_example.py", line 26, in <module>
frame.pivot(index="index", columns="columns", values="values")
~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/ken/no_backup/pivot_example/.venv/lib/python3.13/site-packages/pandas/core/frame.py", line 9366, in pivot
return pivot(self, index=index, columns=columns, values=values)
File "/home/ken/no_backup/pivot_example/.venv/lib/python3.13/site-packages/pandas/core/reshape/pivot.py", line 570, in pivot
result = indexed.unstack(columns_listlike) # type: ignore[arg-type]
File "/home/ken/no_backup/pivot_example/.venv/lib/python3.13/site-packages/pandas/core/series.py", line 4634, in unstack
return unstack(self, level, fill_value, sort)
File "/home/ken/no_backup/pivot_example/.venv/lib/python3.13/site-packages/pandas/core/reshape/reshape.py", line 520, in unstack
return unstacker.get_result(
~~~~~~~~~~~~~~~~~~~~^
obj._values, value_columns=None, fill_value=fill_value
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/home/ken/no_backup/pivot_example/.venv/lib/python3.13/site-packages/pandas/core/reshape/reshape.py", line 238, in get_result
values, _ = self.get_new_values(values, fill_value)
~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
File "/home/ken/no_backup/pivot_example/.venv/lib/python3.13/site-packages/pandas/core/reshape/reshape.py", line 278, in get_new_values
new_values = np.empty(result_shape, dtype=dtype)
TypeError: Cannot interpret 'period[D]' as a data type
If the DataFrame
is not empty, no error is encountered.
Expected Behavior
No error should occur.
Installed Versions
INSTALLED VERSIONS
commit : 9c8bc3e
python : 3.13.7
python-bits : 64
OS : Linux
OS-release : 6.17.1-arch1-1
Version : #1 SMP PREEMPT_DYNAMIC Mon, 06 Oct 2025 18:48:29 +0000
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.3.3
numpy : 2.3.4
pytz : 2025.2
dateutil : 2.9.0.post0
pip : 25.2
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 : None
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 : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.2
qtpy : None
pyqt5 : None