Skip to content

Commit 8ba9b6f

Browse files
use infer_dtype_from_scalar
1 parent afa8775 commit 8ba9b6f

File tree

3 files changed

+7
-19
lines changed

3 files changed

+7
-19
lines changed

pandas/core/dtypes/cast.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def maybe_promote(dtype, fill_value=np.nan):
333333
return dtype, fill_value
334334

335335

336-
def infer_dtype_from_scalar(val, pandas_dtype=False):
336+
def infer_dtype_from_scalar(val, pandas_dtype=False, use_datetimetz=True):
337337
"""
338338
interpret the dtype from a scalar
339339
@@ -368,7 +368,7 @@ def infer_dtype_from_scalar(val, pandas_dtype=False):
368368

369369
elif isinstance(val, (np.datetime64, datetime)):
370370
val = tslib.Timestamp(val)
371-
if val is tslib.NaT or val.tz is None:
371+
if val is tslib.NaT or val.tz is None or not use_datetimetz:
372372
dtype = np.dtype('M8[ns]')
373373
else:
374374
if pandas_dtype:

pandas/core/util/hashing.py

+3-15
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import numpy as np
77
from pandas._libs import hashing
8-
from pandas.compat import string_and_binary_types, text_type
98
from pandas.core.dtypes.generic import (
109
ABCMultiIndex,
1110
ABCIndexClass,
@@ -14,6 +13,7 @@
1413
from pandas.core.dtypes.common import (
1514
is_categorical_dtype, is_list_like)
1615
from pandas.core.dtypes.missing import isnull
16+
from pandas.core.dtypes.cast import infer_dtype_from_scalar
1717

1818

1919
# 16 byte long hashing key
@@ -317,20 +317,8 @@ def _hash_scalar(val, encoding='utf8', hash_key=None):
317317
# this is to be consistent with the _hash_categorical implementation
318318
return np.array([np.iinfo(np.uint64).max], dtype='u8')
319319

320-
if isinstance(val, string_and_binary_types + (text_type,)):
321-
vals = np.array([val], dtype=object)
322-
else:
323-
vals = np.array([val])
324-
325-
if vals.dtype == np.object_:
326-
from pandas import Timestamp, Timedelta, Period, Interval
327-
if isinstance(val, (Timestamp, Timedelta)):
328-
vals = np.array([val.value])
329-
elif isinstance(val, (Period, Interval)):
330-
pass
331-
else:
332-
from pandas import Index
333-
vals = Index(vals).values
320+
dtype, val = infer_dtype_from_scalar(val, use_datetimetz=False)
321+
vals = np.array([val], dtype=dtype)
334322

335323
return hash_array(vals, hash_key=hash_key, encoding=encoding,
336324
categorize=False)

pandas/tests/util/test_hashing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ def test_hash_tuples(self):
8181

8282
def test_hash_tuple(self):
8383
# test equivalence between hash_tuples and hash_tuple
84-
for tup in [(1, 'one'), (1, np.nan)]:
84+
for tup in [(1, 'one'), (1, np.nan), (1.0, pd.NaT, 'A')]:
8585
result = hash_tuple(tup)
8686
expected = hash_tuples([tup])[0]
8787
assert result == expected
8888

8989
def test_hash_scalar(self):
90-
for val in [1, 1.4, 'A', b'A', u'A', pd.Timestamp("2012-01-01"),
90+
for val in [1, 1.4, 'A', b'A', u'A', pd.Timestamp("2012-01-01"),
9191
pd.Timestamp("2012-01-01", tz='Europe/Brussels'),
9292
pd.Period('2012-01-01', freq='D'), pd.Timedelta('1 days'),
9393
pd.Interval(0, 1), np.nan, pd.NaT, None]:

0 commit comments

Comments
 (0)