Skip to content

Commit 43b0473

Browse files
committed
Merge pull request #7515 from sinhrks/areabug
BUG: area plot raises ValueError with tz-aware data
2 parents c8e9b40 + 6ccc8dc commit 43b0473

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

doc/source/v0.14.1.txt

+2
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ Bug Fixes
262262

263263

264264

265+
- Bug in area plot with tz-aware time series raises ``ValueError`` (:issue:`7471`)
266+
265267
- Bug in non-monotonic ``Index.union`` may preserve ``name`` incorrectly (:issue:`7458`)
266268
- Bug in ``DatetimeIndex.intersection`` doesn't preserve timezone (:issue:`4690`)
267269

pandas/tests/test_graphics.py

+27-3
Original file line numberDiff line numberDiff line change
@@ -460,9 +460,33 @@ def test_plot_figsize_and_title(self):
460460
def test_ts_area_lim(self):
461461
ax = self.ts.plot(kind='area', stacked=False)
462462
xmin, xmax = ax.get_xlim()
463-
lines = ax.get_lines()
464-
self.assertEqual(xmin, lines[0].get_data(orig=False)[0][0])
465-
self.assertEqual(xmax, lines[0].get_data(orig=False)[0][-1])
463+
line = ax.get_lines()[0].get_data(orig=False)[0]
464+
self.assertEqual(xmin, line[0])
465+
self.assertEqual(xmax, line[-1])
466+
tm.close()
467+
468+
# GH 7471
469+
ax = self.ts.plot(kind='area', stacked=False, x_compat=True)
470+
xmin, xmax = ax.get_xlim()
471+
line = ax.get_lines()[0].get_data(orig=False)[0]
472+
self.assertEqual(xmin, line[0])
473+
self.assertEqual(xmax, line[-1])
474+
tm.close()
475+
476+
tz_ts = self.ts.copy()
477+
tz_ts.index = tz_ts.tz_localize('GMT').tz_convert('CET')
478+
ax = tz_ts.plot(kind='area', stacked=False, x_compat=True)
479+
xmin, xmax = ax.get_xlim()
480+
line = ax.get_lines()[0].get_data(orig=False)[0]
481+
self.assertEqual(xmin, line[0])
482+
self.assertEqual(xmax, line[-1])
483+
tm.close()
484+
485+
ax = tz_ts.plot(kind='area', stacked=False, secondary_y=True)
486+
xmin, xmax = ax.get_xlim()
487+
line = ax.get_lines()[0].get_data(orig=False)[0]
488+
self.assertEqual(xmin, line[0])
489+
self.assertEqual(xmax, line[-1])
466490

467491
def test_line_area_nan_series(self):
468492
values = [1, 2, np.nan, 3]

pandas/tools/plotting.py

+6-13
Original file line numberDiff line numberDiff line change
@@ -1767,7 +1767,9 @@ def _post_plot_logic(self):
17671767
else:
17681768
if self.xlim is None:
17691769
for ax in self.axes:
1770-
ax.set_xlim(0, len(self.data)-1)
1770+
lines = _get_all_lines(ax)
1771+
left, right = _get_xlim(lines)
1772+
ax.set_xlim(left, right)
17711773

17721774
if self.ylim is None:
17731775
if (self.data >= 0).all().all():
@@ -3083,21 +3085,12 @@ def _get_all_lines(ax):
30833085
def _get_xlim(lines):
30843086
left, right = np.inf, -np.inf
30853087
for l in lines:
3086-
x = l.get_xdata()
3087-
left = min(_maybe_convert_date(x[0]), left)
3088-
right = max(_maybe_convert_date(x[-1]), right)
3088+
x = l.get_xdata(orig=False)
3089+
left = min(x[0], left)
3090+
right = max(x[-1], right)
30893091
return left, right
30903092

30913093

3092-
def _maybe_convert_date(x):
3093-
if not com.is_integer(x):
3094-
conv_func = conv._dt_to_float_ordinal
3095-
if isinstance(x, datetime.time):
3096-
conv_func = conv._to_ordinalf
3097-
x = conv_func(x)
3098-
return x
3099-
3100-
31013094
if __name__ == '__main__':
31023095
# import pandas.rpy.common as com
31033096
# sales = com.load_data('sanfrancisco.home.sales', package='nutshell')

0 commit comments

Comments
 (0)