8000 FIX/ENH: attempt soft conversion of object series before raising a TypeError when plotting by cpcloud · Pull Request #3912 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

FIX/ENH: attempt soft conversion of object series before raising a TypeError when plotting #3912

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 4 commits into from
Jun 16, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
DOC: add release notes/whatsnew
  • Loading branch information
cpcloud committed Jun 15, 2013
commit 6eeff1a7c8256d37edc74ff0542fd305b6c6afcb
14 changes: 8 additions & 6 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ pandas 0.11.1
dependencies offered for Linux) (GH3837_).
- Plotting functions now raise a ``TypeError`` before trying to plot anything
if the associated objects have have a dtype of ``object`` (GH1818_,
GH3572_). This happens before any drawing takes place which elimnates any
spurious plots from showing up.
GH3572_, GH3911_, GH3912_), but they will try to convert object arrays to
numeric arrays if possible so that you can still plot, for example, an
object array with floats. This happens before any drawing takes place which
elimnates any spurious plots from showing up.
- Added Faq section on repr display options, to help users customize their setup.
- ``where`` operations that result in block splitting are much faster (GH3733_)
- Series and DataFrame hist methods now take a ``figsize`` argument (GH3834_)
Expand Down Expand Up @@ -341,13 +343,13 @@ pandas 0.11.1
.. _GH3834: https://github.com/pydata/pandas/issues/3834
.. _GH3873: https://github.com/pydata/pandas/issues/3873
.. _GH3877: https://github.com/pydata/pandas/issues/3877
.. _GH3659: https://github.com/pydata/pandas/issues/3659
.. _GH3679: https://github.com/pydata/pandas/issues/3679
.. _GH3880: https://github.com/pydata/pandas/issues/3880
<<<<<<< HEAD
.. _GH3911: https://github.com/pydata/pandas/issues/3911
=======
.. _GH3907: https://github.com/pydata/pandas/issues/3907
>>>>>>> 7b5933247b80174de4ba571e95a1add809dd9d09

.. _GH3911: https://github.com/pydata/pandas/issues/3911
.. _GH3912: https://github.com/pydata/pandas/issues/3912

pandas 0.11.0
=============
Expand Down
10 changes: 7 additions & 3 deletions doc/source/v0.11.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,11 @@ Bug Fixes
~~~~~~~~~

- Plotting functions now raise a ``TypeError`` before trying to plot anything
if the associated objects have have a ``dtype`` of ``object`` (GH1818_,
GH3572_). This happens before any drawing takes place which elimnates any
spurious plots from showing up.
if the associated objects have have a dtype of ``object`` (GH1818_,
GH3572_, GH3911_, GH3912_), but they will try to convert object arrays to
numeric arrays if possible so that you can still plot, for example, an
object array with floats. This happens before any drawing takes place which
elimnates any spurious plots from showing up.

- ``fillna`` methods now raise a ``TypeError`` if the ``value`` parameter is
a list or tuple.
Expand Down Expand Up @@ -416,3 +418,5 @@ on GitHub for a complete list.
.. _GH3659: https://github.com/pydata/pandas/issues/3659
.. _GH3679: https://github.com/pydata/pandas/issues/3679
.. _GH3907: https://github.com/pydata/pandas/issues/3907
.. _GH3911: https://github.com/pydata/pandas/issues/3911
.. _GH3912: https://github.com/pydata/pandas/issues/3912
5 changes: 4 additions & 1 deletion pandas/tests/test_graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pandas.util.testing import ensure_clean
from pandas.core.config import set_option


import numpy as np

from numpy.testing import assert_array_equal
Expand Down Expand Up @@ -198,11 +199,13 @@ def test_invalid_plot_data(self):

@slow
def test_valid_object_plot(self):
from pandas.io.pytables import PerformanceWarning
s = Series(range(10), dtype=object)
kinds = 'line', 'bar', 'barh', 'kde', 'density'

for kind in kinds:
_check_plot_works(s.plot, kind=kind)
tm.assert_warns(PerformanceWarning, _check_plot_works, s.plot,
kind=kind)

def test_partially_invalid_plot_data(self):
s = Series(['a', 'b', 1.0, 2])
Expand Down
19 changes: 14 additions & 5 deletions pandas/tools/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,16 +877,25 @@ def _get_layout(self):
return (len(self.data.columns), 1)

def _compute_plot_data(self):
from pandas.io.pytables import PerformanceWarning
try:
# might be a frame
numeric_data = self.data._get_numeric_data()
except AttributeError:
# attempt soft conversion
numeric_data = self.data.convert_objects()
numeric_data = self.data
orig_dtype = numeric_data.dtype

# a series, but no object dtypes allowed!
if numeric_data.dtype == np.object_:
raise TypeError('invalid dtype for plotting')
if orig_dtype == np.object_:
# attempt soft conversion, but raise a perf warning
numeric_data = numeric_data.convert_objects()
num_data_dtype = numeric_data.dtype

if num_data_dtype == np.object_:
raise TypeError('No numeric data to plot')
else:
warnings.warn('Coerced object dtype to numeric dtype, '
'you should avoid object dtyped Series if '
'possible', PerformanceWarning)

try:
is_empty = numeric_data.empty
Expand Down
13 changes: 13 additions & 0 deletions pandas/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import string
import sys
import tempfile
import warnings

from contextlib import contextmanager # contextlib is available since 2.5

Expand Down Expand Up @@ -746,3 +747,15 @@ def stdin_encoding(encoding=None):
sys.stdin = SimpleMock(sys.stdin, "encoding", encoding)
yield
sys.stdin = _stdin


def assert_warns(warning, f, *args, **kwargs):
"""
From: http://stackoverflow.com/questions/3892218/how-to-test-with-pythons-unittest-that-a-warning-has-been-thrown
"""
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter('always')
f(*args, **kwargs)
msg = '{0!r} not raised'.format(warning)
assert any(issubclass(item.category, warning)
for item in warning_list), msg
0