Skip to content

Commit 54edf6b

Browse files
fangchenliclaude
andcommitted
ENH: Apply TimeSeries_DateFormatter to bar plots with datetime indices (GH#1918)
Bar plots now use TimeSeries_DateFormatter for datetime and period indices, matching the behavior of line plots. This provides consistent date formatting across different plot types instead of converting indices to string labels. Also update test_memory_leak to use 'D' (daily) frequency instead of deprecated 'B' (business day) frequency to avoid FutureWarning. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 6537beb commit 54edf6b

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

pandas/plotting/_matplotlib/core.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,27 +2045,27 @@ def _post_plot_logic(self, ax: Axes, data) -> None:
20452045
s_edge = self.ax_pos[0] - 0.25 + self.lim_offset
20462046
e_edge = self.ax_pos[-1] + 0.25 + self.bar_width + self.lim_offset
20472047

2048-
# GH#1918: Use date formatter for time series indices
2048+
# GH#1918: Apply date formatter for time series indices
20492049
if self._is_ts_plot():
2050-
ax.set_xlim((s_edge, e_edge))
2051-
2052-
if self.xticks is not None:
2053-
ax.set_xticks(np.array(self.xticks))
2054-
else:
2055-
ax.set_xticks(self.tick_pos)
2056-
2057-
if self._get_index_name() is not None and self.use_index:
2058-
ax.set_xlabel(self._get_index_name())
2059-
2050+
decorate_axes(ax, data.index.freq)
20602051
freq = data.index.freq
2061-
decorate_axes(ax, freq)
20622052

20632053
index = data.index
20642054
if isinstance(index, ABCDatetimeIndex):
20652055
index = index.to_period(freq=freq)
20662056

2067-
if isinstance(index, (ABCPeriodIndex,)):
2057+
if isinstance(index, ABCPeriodIndex):
20682058
format_dateaxis(ax, freq, index)
2059+
2060+
ax.set_xlim((s_edge, e_edge))
2061+
if self.xticks is not None:
2062+
ax.set_xticks(np.array(self.xticks))
2063+
else:
2064+
ax.set_xticks(self.tick_pos)
2065+
2066+
index_name = self._get_index_name()
2067+
if index_name is not None and self.use_index:
2068+
ax.set_xlabel(index_name)
20692069
else:
20702070
if self.use_index:
20712071
str_index = [pprint_thing(key) for key in data.index]

pandas/tests/plotting/frame/test_frame.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2155,13 +2155,13 @@ def test_memory_leak(self, kind):
21552155
df = DataFrame(
21562156
np.random.default_rng(2).standard_normal((10, 4)),
21572157
columns=Index(list("ABCD"), dtype=object),
2158-
index=date_range("2000-01-01", periods=10, freq="B"),
2158+
index=date_range("2000-01-01", periods=10, freq="D"),
21592159
).abs()
21602160
else:
21612161
df = DataFrame(
21622162
np.random.default_rng(2).standard_normal((10, 4)),
21632163
columns=Index(list("ABCD"), dtype=object),
2164-
index=date_range("2000-01-01", periods=10, freq="B"),
2164+
index=date_range("2000-01-01", periods=10, freq="D"),
21652165
)
21662166

21672167
# Use a weakref so we can see if the object gets collected without

0 commit comments

Comments
 (0)