pandas plotting timedelta with yerr fails

Viewed 17

I want to plot a pandas series like so:

s = pd.Series(
    [1, 2, 3, 4, 5],
    index=pd.TimedeltaIndex(
        ["0 days 00:30:00", "0 days 01:00:00", "0 days 01:30:00", "0 days 02:00:00", "0 days 02:30:00"],
        dtype="timedelta64[ns]",
        freq=None,
    ),
)

s.plot(yerr=s)

Without yerr=s the above works fine. When I pass in yerr=s I get:

...
     16 @set_module('numpy')
     17 def asarray(a, dtype=None, order=None):
     18     """Convert the input to an array.
     19 
     20     Parameters
   (...)
     83 
     84     """
---> 85     return array(a, dtype, copy=False, order=order)

ValueError: Could not convert object to NumPy timedelta

If we defined s without timedelta indices, it would work:

s = pd.Series(
    [1, 2, 3, 4, 5],
    index=[1, 2, 3, 4, 5]
)

enter image description here

What's going on here? How can I add the error bars to my line graph?

1 Answers

Your example code works fine for me... this means you'll probably need to look into more details such as your pandas, numpy, and matplotlib versions.

import pandas as pd
import matplotlib.pyplot as plt

s = pd.Series(
    [1, 2, 3, 4, 5],
    index=pd.TimedeltaIndex(
        ["0 days 00:30:00", "0 days 01:00:00", "0 days 01:30:00", "0 days 02:00:00", "0 days 02:30:00"],
        dtype="timedelta64[ns]",
        freq=None,
    ),
)

s.plot(yerr=s)
plt.show()

Output:

enter image description here

Related