Skip to content

BUG: Area plot for timedelta raises error _UFuncInputCastingError #52246

Open
@SlowMo24

Description

@SlowMo24

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 pandas as pd

data = pd.DataFrame(data=pd.timedelta_range(start =pd.Timedelta(0), periods=5, freq="h"))

data.plot.area()
Traceback (most recent call last):
  File "report.py", line 10, in <module>
    data.plot.area()
  File ".venv/lib/python3.10/site-packages/pandas/plotting/_core.py", line 1546, in area
    return self(kind="area", x=x, y=y, stacked=stacked, **kwargs)
  File ".venv/lib/python3.10/site-packages/pandas/plotting/_core.py", line 981, in __call__
    return plot_backend.plot(data, kind=kind, **kwargs)
  File ".venv/lib/python3.10/site-packages/pandas/plotting/_matplotlib/__init__.py", line 71, in plot
    plot_obj.generate()
  File ".venv/lib/python3.10/site-packages/pandas/plotting/_matplotlib/core.py", line 449, in generate
    self._make_plot()
  File ".venv/lib/python3.10/site-packages/pandas/plotting/_matplotlib/core.py", line 1383, in _make_plot
    newlines = plotf(
  File ".venv/lib/python3.10/site-packages/pandas/plotting/_matplotlib/core.py", line 1557, in _plot
    y_values = cls._get_stacked_values(ax, stacking_id, y, kwds["label"])
  File ".venv/lib/python3.10/site-packages/pandas/plotting/_matplotlib/core.py", line 1461, in _get_stacked_values
    if (values >= 0).all():
numpy.core._exceptions._UFuncInputCastingError: Cannot cast ufunc 'greater_equal' input 0 from dtype('<m8[ns]') to dtype('<m8') with casting rule 'same_kind'

Issue Description

Creating an area plot of timedeltas fails _UFuncInputCastingError on the main branch. The line plot works fine data.plot.line().

In the latest version of pandas, the line-plot also works fine but the area plot throws:

report.py:10: FutureWarning: The behavior of fillna with timedelta64[ns] dtype and an incompatible value (<class 'int'>) is deprecated. In a future version, this will cast to a common dtype (usually object) instead of raising, matching the behavior of other dtypes.
  data.plot.area()
Traceback (most recent call last):
  File "report.py", line 10, in <module>
    data.plot.area()
  File ".venv/lib/python3.10/site-packages/pandas/plotting/_core.py", line 1557, in area
    return self(kind="area", x=x, y=y, **kwargs)
  File ".venv/lib/python3.10/site-packages/pandas/plotting/_core.py", line 1000, in __call__
    return plot_backend.plot(data, kind=kind, **kwargs)
  File ".venv/lib/python3.10/site-packages/pandas/plotting/_matplotlib/__init__.py", line 70, in plot
    plot_obj = PLOT_CLASSES[kind](data, **kwargs)
  File ".venv/lib/python3.10/site-packages/pandas/plotting/_matplotlib/core.py", line 1529, in __init__
    data = data.fillna(value=0)
  File ".venv/lib/python3.10/site-packages/pandas/util/_decorators.py", line 331, in wrapper
    return func(*args, **kwargs)
  File ".venv/lib/python3.10/site-packages/pandas/core/frame.py", line 5635, in fillna
    return super().fillna(
  File ".venv/lib/python3.10/site-packages/pandas/core/generic.py", line 6922, in fillna
    new_data = self._mgr.fillna(
  File ".venv/lib/python3.10/site-packages/pandas/core/internals/managers.py", line 443, in fillna
    return self.apply(
  File "/.venv/lib/python3.10/site-packages/pandas/core/internals/managers.py", line 352, in apply
    applied = getattr(b, f)(**kwargs)
  File ".venv/lib/python3.10/site-packages/pandas/core/internals/blocks.py", line 1566, in fillna
    res_values = self.values.fillna(value, limit=limit)
  File ".venv/lib/python3.10/site-packages/pandas/core/arrays/_mixins.py", line 346, in fillna
    self._validate_setitem_value(value)
  File ".venv/lib/python3.10/site-packages/pandas/core/arrays/datetimelike.py", line 789, in _validate_setitem_value
    return self._validate_scalar(value, allow_listlike=True)
  File "venv/lib/python3.10/site-packages/pandas/core/arrays/datetimelike.py", line 690, in _validate_scalar
    raise TypeError(msg)
TypeError: value should be a 'Timedelta', 'NaT', or array of those. Got 'int' instead.

This seems to be resolved by #48103 .

Expected Behavior

Plot an area plot, similar to the line plot.

Workaround (from https://stackoverflow.com/questions/23543909/plotting-pandas-timedelta):

Convert the timedelta into a numeric value by dividing by a fixed timedelta of your choice: data = data/pd.Timedelta(hours=1)

Installed Versions

.venv/lib/python3.10/site-packages/_distutils_hack/__init__.py:33: UserWarning: Setuptools is replacing distutils. warnings.warn("Setuptools is replacing distutils.") INSTALLED VERSIONS ------------------ commit : 5c4cc4a python : 3.10.10.final.0 python-bits : 64 OS : Linux OS-release : 5.15.0-67-generic Version : #74~20.04.1-Ubuntu SMP Wed Feb 22 14:52:34 UTC 2023 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : de_DE.UTF-8 LOCALE : de_DE.UTF-8 pandas : 2.1.0.dev0+315.g5c4cc4a51c numpy : 1.24.2 pytz : 2022.7.1 dateutil : 2.8.2 setuptools : 65.5.0 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 : 3.7.1 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 zstandard : None tzdata : 2023.2 qtpy : None pyqt5 : None

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions