Skip to content

Commit eee4039

Browse files
authored
BUG: df.plot with a PeriodIndex causes a shift to the right when the frequency multiplier is greater than one (#58322)
* bug plots makes shift for period_range * add a note to v3.0.0, correct test_plot_period_index_makes_no_right_shift
1 parent 1fbe6f3 commit eee4039

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ Period
417417
Plotting
418418
^^^^^^^^
419419
- Bug in :meth:`.DataFrameGroupBy.boxplot` failed when there were multiple groupings (:issue:`14701`)
420+
- Bug in :meth:`DataFrame.plot` that causes a shift to the right when the frequency multiplier is greater than one. (:issue:`57587`)
420421
-
421422

422423
Groupby/resample/rolling

pandas/plotting/_matplotlib/timeseries.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ def maybe_convert_index(ax: Axes, data: NDFrameT) -> NDFrameT:
310310
if isinstance(data.index, ABCDatetimeIndex):
311311
data = data.tz_localize(None).to_period(freq=freq_str)
312312
elif isinstance(data.index, ABCPeriodIndex):
313-
data.index = data.index.asfreq(freq=freq_str)
313+
data.index = data.index.asfreq(freq=freq_str, how="start")
314314
return data
315315

316316

pandas/tests/plotting/frame/test_frame.py

+15
Original file line numberDiff line numberDiff line change
@@ -2578,6 +2578,21 @@ def test_plot_no_warning(self):
25782578
_ = df.plot()
25792579
_ = df.T.plot()
25802580

2581+
@pytest.mark.parametrize("freq", ["h", "7h", "60min", "120min", "3M"])
2582+
def test_plot_period_index_makes_no_right_shift(self, freq):
2583+
# GH#57587
2584+
idx = pd.period_range("01/01/2000", freq=freq, periods=4)
2585+
df = DataFrame(
2586+
np.array([0, 1, 0, 1]),
2587+
index=idx,
2588+
columns=["A"],
2589+
)
2590+
expected = idx.values
2591+
2592+
ax = df.plot()
2593+
result = ax.get_lines()[0].get_xdata()
2594+
assert all(str(result[i]) == str(expected[i]) for i in range(4))
2595+
25812596

25822597
def _generate_4_axes_via_gridspec():
25832598
import matplotlib.pyplot as plt

0 commit comments

Comments
 (0)