Skip to content

BUG: area plot raises ValueError with tz-aware data #7515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/v0.14.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ Bug Fixes



- Bug in area plot with tz-aware time series raises ``ValueError`` (:issue:`7471`)

- Bug in non-monotonic ``Index.union`` may preserve ``name`` incorrectly (:issue:`7458`)
- Bug in ``DatetimeIndex.intersection`` doesn't preserve timezone (:issue:`4690`)

Expand Down
30 changes: 27 additions & 3 deletions pandas/tests/test_graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,9 +460,33 @@ def test_plot_figsize_and_title(self):
def test_ts_area_lim(self):
ax = self.ts.plot(kind='area', stacked=False)
xmin, xmax = ax.get_xlim()
lines = ax.get_lines()
self.assertEqual(xmin, lines[0].get_data(orig=False)[0][0])
self.assertEqual(xmax, lines[0].get_data(orig=False)[0][-1])
line = ax.get_lines()[0].get_data(orig=False)[0]
self.assertEqual(xmin, line[0])
self.assertEqual(xmax, line[-1])
tm.close()

# GH 7471
ax = self.ts.plot(kind='area', stacked=False, x_compat=True)
xmin, xmax = ax.get_xlim()
line = ax.get_lines()[0].get_data(orig=False)[0]
self.assertEqual(xmin, line[0])
self.assertEqual(xmax, line[-1])
tm.close()

tz_ts = self.ts.copy()
tz_ts.index = tz_ts.tz_localize('GMT').tz_convert('CET')
ax = tz_ts.plot(kind='area', stacked=False, x_compat=True)
xmin, xmax = ax.get_xlim()
line = ax.get_lines()[0].get_data(orig=False)[0]
self.assertEqual(xmin, line[0])
self.assertEqual(xmax, line[-1])
tm.close()

ax = tz_ts.plot(kind='area', stacked=False, secondary_y=True)
xmin, xmax = ax.get_xlim()
line = ax.get_lines()[0].get_data(orig=False)[0]
self.assertEqual(xmin, line[0])
self.assertEqual(xmax, line[-1])

def test_line_area_nan_series(self):
values = [1, 2, np.nan, 3]
Expand Down
19 changes: 6 additions & 13 deletions pandas/tools/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1767,7 +1767,9 @@ def _post_plot_logic(self):
else:
if self.xlim is None:
for ax in self.axes:
ax.set_xlim(0, len(self.data)-1)
lines = _get_all_lines(ax)
left, right = _get_xlim(lines)
ax.set_xlim(left, right)

if self.ylim is None:
if (self.data >= 0).all().all():
Expand Down Expand Up @@ -3083,21 +3085,12 @@ def _get_all_lines(ax):
def _get_xlim(lines):
left, right = np.inf, -np.inf
for l in lines:
x = l.get_xdata()
left = min(_maybe_convert_date(x[0]), left)
right = max(_maybe_convert_date(x[-1]), right)
x = l.get_xdata(orig=False)
left = min(x[0], left)
right = max(x[-1], right)
return left, right


def _maybe_convert_date(x):
if not com.is_integer(x):
conv_func = conv._dt_to_float_ordinal
if isinstance(x, datetime.time):
conv_func = conv._to_ordinalf
x = conv_func(x)
return x


if __name__ == '__main__':
# import pandas.rpy.common as com
# sales = com.load_data('sanfrancisco.home.sales', package='nutshell')
Expand Down