Skip to content

Commit

Permalink
TST: fixed up plotting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jreback committed Aug 3, 2014
1 parent 89a7727 commit 2f32f46
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 17 deletions.
7 changes: 4 additions & 3 deletions doc/source/v0.15.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ users upgrade to this version.

.. warning::

In 0.15.0 ``Index`` has internaly been refactored to no longer sub-class ``ndarray``
In 0.15.0 ``Index`` has internally been refactored to no longer sub-class ``ndarray``
but instead subclass ``PandasObject``, similarly to the rest of the pandas objects. This change allows very easy sub-classing and creation of new index types. This should be
a transparent change with only very limited API implications (See the :ref:`Internal Refactoring <whatsnew_0150.refactoring>`)

Expand Down Expand Up @@ -141,7 +141,7 @@ API changes
Internal Refactoring
~~~~~~~~~~~~~~~~~~~~

In 0.15.0 ``Index`` has internaly been refactored to no longer sub-class ``ndarray``
In 0.15.0 ``Index`` has internally been refactored to no longer sub-class ``ndarray``
but instead subclass ``PandasObject``, similarly to the rest of the pandas objects. This change allows very easy sub-classing and creation of new index types. This should be
a transparent change with only very limited API implications (:issue:`5080`,:issue:`7439`,:issue:`7796`)

Expand All @@ -158,6 +158,7 @@ a transparent change with only very limited API implications (:issue:`5080`,:iss
arr<=idx
arr>idx

- when plotting with a ``PeriodIndex``. The ``matplotlib`` internal axes will now be arrays of ``Period`` rather than a ``PeriodIndex``. (this is similar to how a ``DatetimeIndex`` passess arrays of ``datetimes`` now)
- ``:func:`~pandas.core.index.MultiIndex.repeat` now is available

.. _whatsnew_0150.cat:
Expand Down Expand Up @@ -284,7 +285,7 @@ Performance
~~~~~~~~~~~

- Performance improvements in ``DatetimeIndex.__iter__`` to allow faster iteration (:issue:`7683`)

- Performance improvements in ``Period`` creation (and ``PeriodIndex`` setitem) (:issue:`5155`)



Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/test_graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,8 @@ def test_xcompat(self):
pd.plot_params['x_compat'] = False
ax = df.plot()
lines = ax.get_lines()
tm.assert_isinstance(lines[0].get_xdata(), PeriodIndex)
self.assertNotIsInstance(lines[0].get_xdata(), PeriodIndex)
self.assertIsInstance(PeriodIndex(lines[0].get_xdata()), PeriodIndex)

tm.close()
# useful if you're plotting a bunch together
Expand All @@ -1012,7 +1013,8 @@ def test_xcompat(self):
tm.close()
ax = df.plot()
lines = ax.get_lines()
tm.assert_isinstance(lines[0].get_xdata(), PeriodIndex)
self.assertNotIsInstance(lines[0].get_xdata(), PeriodIndex)
self.assertIsInstance(PeriodIndex(lines[0].get_xdata()), PeriodIndex)

def test_unsorted_index(self):
df = DataFrame({'y': np.arange(100)},
Expand Down
2 changes: 1 addition & 1 deletion pandas/tseries/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def convert(values, units, axis):
if isinstance(values, Index):
return values.map(lambda x: get_datevalue(x, axis.freq))
if isinstance(values, (list, tuple, np.ndarray, Index)):
return [get_datevalue(x, axis.freq) for x in values]
return PeriodIndex(values, freq=axis.freq).values
return values


Expand Down
14 changes: 10 additions & 4 deletions pandas/tseries/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ class Period(PandasObject):
__slots__ = ['freq', 'ordinal']
_comparables = ['name','freqstr']

@classmethod
def from_ordinal(cls, ordinal, freq):
""" fast creation from an ordinal and freq that are already validated! """
self = object.__new__(cls)
self.ordinal = ordinal
self.freq = freq
return self

def __init__(self, value=None, freq=None, ordinal=None,
year=None, month=1, quarter=None, day=1,
hour=0, minute=0, second=0):
Expand Down Expand Up @@ -705,7 +713,7 @@ def __contains__(self, key):

@property
def _box_func(self):
return lambda x: Period(ordinal=x, freq=self.freq)
return lambda x: Period.from_ordinal(ordinal=x, freq=self.freq)

def asof_locs(self, where, mask):
"""
Expand Down Expand Up @@ -809,9 +817,7 @@ def map(self, f):

def _get_object_array(self):
freq = self.freq
boxfunc = lambda x: Period(ordinal=x, freq=freq)
boxer = np.frompyfunc(boxfunc, 1, 1)
return boxer(self.values)
return np.array([ Period.from_ordinal(ordinal=x, freq=freq) for x in self.values], copy=False)

def _mpl_repr(self):
# how to represent ourselves to matplotlib
Expand Down
4 changes: 2 additions & 2 deletions pandas/tseries/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def tsplot(series, plotf, **kwargs):
if not hasattr(ax, '_plot_data'):
ax._plot_data = []
ax._plot_data.append((series, plotf, kwargs))
lines = plotf(ax, series.index, series.values, **kwargs)
lines = plotf(ax, series.index._mpl_repr(), series.values, **kwargs)

# set date formatter, locators and rescale limits
format_dateaxis(ax, ax.freq)
Expand Down Expand Up @@ -152,7 +152,7 @@ def _replot_ax(ax, freq, kwargs):
idx = series.index.asfreq(freq, how='S')
series.index = idx
ax._plot_data.append(series)
lines.append(plotf(ax, series.index, series.values, **kwds)[0])
lines.append(plotf(ax, series.index._mpl_repr(), series.values, **kwds)[0])
labels.append(com.pprint_thing(series.name))

return lines, labels
Expand Down
10 changes: 5 additions & 5 deletions pandas/tseries/tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def test_dataframe(self):
bts = DataFrame({'a': tm.makeTimeSeries()})
ax = bts.plot()
idx = ax.get_lines()[0].get_xdata()
assert_array_equal(bts.index.to_period(), idx)
assert_array_equal(bts.index.to_period(), PeriodIndex(idx))

@slow
def test_axis_limits(self):
Expand Down Expand Up @@ -605,8 +605,8 @@ def test_mixed_freq_regular_first(self):
ax = s1.plot()
ax2 = s2.plot(style='g')
lines = ax2.get_lines()
idx1 = lines[0].get_xdata()
idx2 = lines[1].get_xdata()
idx1 = PeriodIndex(lines[0].get_xdata())
idx2 = PeriodIndex(lines[1].get_xdata())
self.assertTrue(idx1.equals(s1.index.to_period('B')))
self.assertTrue(idx2.equals(s2.index.to_period('B')))
left, right = ax2.get_xlim()
Expand Down Expand Up @@ -881,9 +881,9 @@ def test_secondary_upsample(self):
low.plot()
ax = high.plot(secondary_y=True)
for l in ax.get_lines():
self.assertEqual(l.get_xdata().freq, 'D')
self.assertEqual(PeriodIndex(l.get_xdata()).freq, 'D')
for l in ax.right_ax.get_lines():
self.assertEqual(l.get_xdata().freq, 'D')
self.assertEqual(PeriodIndex(l.get_xdata()).freq, 'D')

@slow
def test_secondary_legend(self):
Expand Down

0 comments on commit 2f32f46

Please sign in to comment.