Skip to content

Commit a87d6ea

Browse files
VIS: register datetime64 in matplotlib units registry (GH8614)
1 parent 5a58f04 commit a87d6ea

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

doc/source/whatsnew/v0.15.0.txt

+2-15
Original file line numberDiff line numberDiff line change
@@ -801,21 +801,8 @@ a transparent change with only very limited API implications (:issue:`5080`, :is
801801
- MultiIndexes will now raise similary to other pandas objects w.r.t. truth testing, see :ref:`here <gotchas.truth>` (:issue:`7897`).
802802
- When plotting a DatetimeIndex directly with matplotlib's `plot` function,
803803
the axis labels will no longer be formatted as dates but as integers (the
804-
internal representation of a ``datetime64``). To keep the old behaviour you
805-
should add the :meth:`~DatetimeIndex.to_pydatetime()` method:
806-
807-
.. code-block:: python
808-
809-
import matplotlib.pyplot as plt
810-
df = pd.DataFrame({'col': np.random.randint(1, 50, 60)},
811-
index=pd.date_range("2012-01-01", periods=60))
812-
813-
# this will now format the x axis labels as integers
814-
plt.plot(df.index, df['col'])
815-
816-
# to keep the date formating, convert the index explicitely to python datetime values
817-
plt.plot(df.index.to_pydatetime(), df['col'])
818-
804+
internal representation of a ``datetime64``). **UPDATE** This is fixed
805+
in 0.15.1, see :ref:`here <whatsnew_0151.datetime64_plotting>`.
819806

820807
.. _whatsnew_0150.deprecations:
821808

doc/source/whatsnew/v0.15.1.txt

+17
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ API changes
169169

170170
- added Index properties `is_monotonic_increasing` and `is_monotonic_decreasing` (:issue:`8680`).
171171

172+
172173
.. note:: io.data.Options has been fixed for a change in the format of the Yahoo Options page (:issue:`8612`)
173174

174175
As a result of a change in Yahoo's option page layout, when an expiry date is given,
@@ -202,6 +203,15 @@ API changes
202203

203204
See the Options documentation in :ref:`Remote Data <remote_data.yahoo_options>`
204205

206+
.. _whatsnew_0151.datetime64_plotting:
207+
208+
- pandas now also registers the ``datetime64`` dtype in matplotlib's units registry
209+
to plot such values as datetimes. This is activated once pandas is imported. In
210+
previous versions, plotting an array of ``datetime64`` values will have resulted
211+
in plotted integer values. To keep the previous behaviour, you can do
212+
``del matplotlib.units.registry[np.datetime64]`` (:issue:`8614`).
213+
214+
205215
.. _whatsnew_0151.enhancements:
206216

207217
Enhancements
@@ -283,8 +293,15 @@ Bug Fixes
283293

284294

285295

296+
<<<<<<< HEAD
286297
- Fixed a bug where plotting a column ``y`` and specifying a label would mutate the index name of the original DataFrame (:issue:`8494`)
287298

288299
- Bug in ``date_range`` where partially-specified dates would incorporate current date (:issue:`6961`)
289300

290301
- Bug in Setting by indexer to a scalar value with a mixed-dtype `Panel4d` was failing (:issue:`8702`)
302+
=======
303+
- Fixed a bug where plotting a column ``y`` and specifying a label
304+
would mutate the index name of the DataFrame ``y`` came from (:issue:`8494`)
305+
306+
- Fix regression in plotting of a DatetimeIndex directly with matplotlib (:issue:`8614`).
307+
>>>>>>> VIS: register datetime64 in matplotlib units registry (GH8614)

pandas/tseries/converter.py

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def register():
3030
units.registry[pydt.datetime] = DatetimeConverter()
3131
units.registry[pydt.date] = DatetimeConverter()
3232
units.registry[pydt.time] = TimeConverter()
33+
units.registry[np.datetime64] = DatetimeConverter()
3334

3435

3536
def _to_ordinalf(tm):
@@ -165,6 +166,8 @@ def try_parse(values):
165166

166167
if isinstance(values, (datetime, pydt.date)):
167168
return _dt_to_float_ordinal(values)
169+
elif isinstance(values, np.datetime64):
170+
return _dt_to_float_ordinal(lib.Timestamp(values))
168171
elif isinstance(values, pydt.time):
169172
return dates.date2num(values)
170173
elif (com.is_integer(values) or com.is_float(values)):

pandas/tseries/tests/test_converter.py

+11
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ def test_conversion(self):
5252
rs = self.dtc.convert(Timestamp('2012-1-1'), None, None)
5353
self.assertEqual(rs, xp)
5454

55+
# also testing datetime64 dtype (GH8614)
56+
rs = self.dtc.convert(np.datetime64('2012-01-01'), None, None)
57+
self.assertEqual(rs, xp)
58+
59+
rs = self.dtc.convert(np.datetime64('2012-01-01 00:00:00+00:00'), None, None)
60+
self.assertEqual(rs, xp)
61+
62+
rs = self.dtc.convert(np.array([np.datetime64('2012-01-01 00:00:00+00:00'),
63+
np.datetime64('2012-01-02 00:00:00+00:00')]), None, None)
64+
self.assertEqual(rs[0], xp)
65+
5566
def test_conversion_float(self):
5667
decimals = 9
5768

0 commit comments

Comments
 (0)