Skip to content

BUG: use more generic type inference for fast plotting #15094

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
Jan 12, 2017
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ Other API Changes
- ``pd.read_csv()`` will now issue a ``ParserWarning`` whenever there are conflicting values provided by the ``dialect`` parameter and the user (:issue:`14898`)
- ``pd.read_csv()`` will now raise a ``ValueError`` for the C engine if the quote character is larger than than one byte (:issue:`11592`)
- ``inplace`` arguments now require a boolean value, else a ``ValueError`` is thrown (:issue:`14189`)
- ``pandas.api.types.is_datetime64_ns_dtype`` will now report ``True`` on a tz-aware dtype, similar to ``pandas.api.types.is_datetime64_any_dtype``

.. _whatsnew_0200.deprecations:

Expand Down
11 changes: 8 additions & 3 deletions pandas/tests/types/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
is_datetime64tz_dtype, is_datetimetz,
is_period_dtype, is_period,
is_dtype_equal, is_datetime64_ns_dtype,
is_datetime64_dtype, is_string_dtype,
is_datetime64_dtype,
is_datetime64_any_dtype, is_string_dtype,
_coerce_to_dtype)
import pandas.util.testing as tm

Expand Down Expand Up @@ -132,8 +133,12 @@ def test_coerce_to_dtype(self):
DatetimeTZDtype('ns', 'Asia/Tokyo'))

def test_compat(self):
self.assertFalse(is_datetime64_ns_dtype(self.dtype))
self.assertFalse(is_datetime64_ns_dtype('datetime64[ns, US/Eastern]'))
self.assertTrue(is_datetime64tz_dtype(self.dtype))
self.assertTrue(is_datetime64tz_dtype('datetime64[ns, US/Eastern]'))
self.assertTrue(is_datetime64_any_dtype(self.dtype))
self.assertTrue(is_datetime64_any_dtype('datetime64[ns, US/Eastern]'))
self.assertTrue(is_datetime64_ns_dtype(self.dtype))
self.assertTrue(is_datetime64_ns_dtype('datetime64[ns, US/Eastern]'))
self.assertFalse(is_datetime64_dtype(self.dtype))
self.assertFalse(is_datetime64_dtype('datetime64[ns, US/Eastern]'))

Expand Down
36 changes: 36 additions & 0 deletions pandas/tests/types/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
from pandas.types import inference
from pandas.types.common import (is_timedelta64_dtype,
is_timedelta64_ns_dtype,
is_datetime64_dtype,
is_datetime64_ns_dtype,
is_datetime64_any_dtype,
is_datetime64tz_dtype,
is_number,
is_integer,
is_float,
Expand Down Expand Up @@ -805,6 +809,38 @@ def test_is_float(self):
self.assertFalse(is_float(np.timedelta64(1, 'D')))
self.assertFalse(is_float(Timedelta('1 days')))

def test_is_datetime_dtypes(self):

ts = pd.date_range('20130101', periods=3)
tsa = pd.date_range('20130101', periods=3, tz='US/Eastern')

self.assertTrue(is_datetime64_dtype('datetime64'))
self.assertTrue(is_datetime64_dtype('datetime64[ns]'))
self.assertTrue(is_datetime64_dtype(ts))
self.assertFalse(is_datetime64_dtype(tsa))

self.assertFalse(is_datetime64_ns_dtype('datetime64'))
self.assertTrue(is_datetime64_ns_dtype('datetime64[ns]'))
self.assertTrue(is_datetime64_ns_dtype(ts))
self.assertTrue(is_datetime64_ns_dtype(tsa))

self.assertTrue(is_datetime64_any_dtype('datetime64'))
self.assertTrue(is_datetime64_any_dtype('datetime64[ns]'))
self.assertTrue(is_datetime64_any_dtype(ts))
self.assertTrue(is_datetime64_any_dtype(tsa))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you do the exact same 4 tests for is_datetime64tz_dtype?

self.assertFalse(is_datetime64tz_dtype('datetime64'))
self.assertFalse(is_datetime64tz_dtype('datetime64[ns]'))
self.assertFalse(is_datetime64tz_dtype(ts))
self.assertTrue(is_datetime64tz_dtype(tsa))

for tz in ['US/Eastern', 'UTC']:
dtype = 'datetime64[ns, {}]'.format(tz)
self.assertFalse(is_datetime64_dtype(dtype))
self.assertTrue(is_datetime64tz_dtype(dtype))
self.assertTrue(is_datetime64_ns_dtype(dtype))
self.assertTrue(is_datetime64_any_dtype(dtype))

def test_is_timedelta(self):
self.assertTrue(is_timedelta64_dtype('timedelta64'))
self.assertTrue(is_timedelta64_dtype('timedelta64[ns]'))
Expand Down
23 changes: 21 additions & 2 deletions pandas/tseries/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import nose

import numpy as np
from pandas import Timestamp, Period
from pandas import Timestamp, Period, Index
from pandas.compat import u
import pandas.util.testing as tm
from pandas.tseries.offsets import Second, Milli, Micro
from pandas.tseries.offsets import Second, Milli, Micro, Day
from pandas.compat.numpy import np_datetime64_compat

try:
Expand Down Expand Up @@ -62,6 +62,25 @@ def test_conversion(self):
np_datetime64_compat('2012-01-02 00:00:00+0000')]), None, None)
self.assertEqual(rs[0], xp)

# we have a tz-aware date (constructed to that when we turn to utc it
# is the same as our sample)
ts = (Timestamp('2012-01-01')
.tz_localize('UTC')
.tz_convert('US/Eastern')
)
rs = self.dtc.convert(ts, None, None)
self.assertEqual(rs, xp)

rs = self.dtc.convert(ts.to_pydatetime(), None, None)
self.assertEqual(rs, xp)

rs = self.dtc.convert(Index([ts - Day(1), ts]), None, None)
self.assertEqual(rs[1], xp)

rs = self.dtc.convert(Index([ts - Day(1), ts]).to_pydatetime(),
None, None)
self.assertEqual(rs[1], xp)

def test_conversion_float(self):
decimals = 9

Expand Down
2 changes: 1 addition & 1 deletion pandas/tseries/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -2404,7 +2404,7 @@ def test_to_datetime_tz_psycopg2(self):
i = pd.DatetimeIndex([
'2000-01-01 08:00:00+00:00'
], tz=psycopg2.tz.FixedOffsetTimezone(offset=-300, name=None))
self.assertFalse(is_datetime64_ns_dtype(i))
self.assertTrue(is_datetime64_ns_dtype(i))

# tz coerceion
result = pd.to_datetime(i, errors='coerce')
Expand Down
16 changes: 8 additions & 8 deletions pandas/tseries/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,14 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
arg = np.array(arg, dtype='O')

# these are shortcutable
if is_datetime64_ns_dtype(arg):
if is_datetime64tz_dtype(arg):
if not isinstance(arg, DatetimeIndex):
return DatetimeIndex(arg, tz=tz, name=name)
if utc:
arg = arg.tz_convert(None).tz_localize('UTC')
return arg

elif is_datetime64_ns_dtype(arg):
if box and not isinstance(arg, DatetimeIndex):
try:
return DatetimeIndex(arg, tz=tz, name=name)
Expand All @@ -318,13 +325,6 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):

return arg

elif is_datetime64tz_dtype(arg):
if not isinstance(arg, DatetimeIndex):
return DatetimeIndex(arg, tz=tz, name=name)
if utc:
arg = arg.tz_convert(None).tz_localize('UTC')
return arg

elif unit is not None:
if format is not None:
raise ValueError("cannot specify both format and unit")
Expand Down
7 changes: 5 additions & 2 deletions pandas/types/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,11 @@ def is_datetime64_ns_dtype(arr_or_dtype):
try:
tipo = _get_dtype(arr_or_dtype)
except TypeError:
return False
return tipo == _NS_DTYPE
if is_datetime64tz_dtype(arr_or_dtype):
tipo = _get_dtype(arr_or_dtype.dtype)
else:
return False
return tipo == _NS_DTYPE or getattr(tipo, 'base', None) == _NS_DTYPE


def is_timedelta64_ns_dtype(arr_or_dtype):
Expand Down