Skip to content

Commit

Permalink
BUG: df.plot with a PeriodIndex causes a shift to the right when …
Browse files Browse the repository at this point in the history
…the frequency multiplier is greater than one (pandas-dev#58322)

* bug plots makes shift for period_range

* add a note to v3.0.0, correct test_plot_period_index_makes_no_right_shift
  • Loading branch information
natmokval authored and pmhatre1 committed May 7, 2024
1 parent 6360249 commit 420a101
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ Period
Plotting
^^^^^^^^
- Bug in :meth:`.DataFrameGroupBy.boxplot` failed when there were multiple groupings (:issue:`14701`)
- Bug in :meth:`DataFrame.plot` that causes a shift to the right when the frequency multiplier is greater than one. (:issue:`57587`)
-

Groupby/resample/rolling
Expand Down
2 changes: 1 addition & 1 deletion pandas/plotting/_matplotlib/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def maybe_convert_index(ax: Axes, data: NDFrameT) -> NDFrameT:
if isinstance(data.index, ABCDatetimeIndex):
data = data.tz_localize(None).to_period(freq=freq_str)
elif isinstance(data.index, ABCPeriodIndex):
data.index = data.index.asfreq(freq=freq_str)
data.index = data.index.asfreq(freq=freq_str, how="start")
return data


Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/plotting/frame/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2578,6 +2578,21 @@ def test_plot_no_warning(self):
_ = df.plot()
_ = df.T.plot()

@pytest.mark.parametrize("freq", ["h", "7h", "60min", "120min", "3M"])
def test_plot_period_index_makes_no_right_shift(self, freq):
# GH#57587
idx = pd.period_range("01/01/2000", freq=freq, periods=4)
df = DataFrame(
np.array([0, 1, 0, 1]),
index=idx,
columns=["A"],
)
expected = idx.values

ax = df.plot()
result = ax.get_lines()[0].get_xdata()
assert all(str(result[i]) == str(expected[i]) for i in range(4))


def _generate_4_axes_via_gridspec():
import matplotlib.pyplot as plt
Expand Down

0 comments on commit 420a101

Please sign in to comment.