Skip to content

Commit 30a13b9

Browse files
committed
all-nan inferred to correct dtype
1 parent 7f0e35e commit 30a13b9

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

doc/source/whatsnew/v0.22.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Other API Changes
7575
- :class:`CacheableOffset` and :class:`WeekDay` are no longer available in the ``pandas.tseries.offsets`` module (:issue:`17830`)
7676
- `tseries.frequencies.get_freq_group()` and `tseries.frequencies.DAYS` are removed from the public API (:issue:`18034`)
7777
- :func:`Series.truncate` and :func:`DataFrame.truncate` will raise a ``ValueError`` if the index is not sorted instead of an unhelpful ``KeyError`` (:issue:`17935`)
78-
- :func:`Index.map` can now accept ``Series`` and dictionary input objects (:issue:`12756`).
78+
- :func:`Index.map` can now accept ``Series`` and dictionary input objects (:issue:`12756`, :issue:`18482`).
7979
- :func:`Dataframe.unstack` will now default to filling with ``np.nan`` for ``object`` columns. (:issue:`12815`)
8080
- :class:`IntervalIndex` constructor will raise if the ``closed`` parameter conflicts with how the input data is inferred to be closed (:issue:`18421`)
8181
- Inserting missing values into indexes will work for all types of indexes and automatically insert the correct type of missing value (``NaN``, ``NaT``, etc.) regardless of the type passed in (:issue:`18295`)

pandas/core/indexes/base.py

+13
Original file line numberDiff line numberDiff line change
@@ -2912,6 +2912,8 @@ def map(self, mapper, na_action=None):
29122912
mapper, na_action=na_action)
29132913

29142914
attributes = self._get_attributes_dict()
2915+
2916+
# we can return a MultiIndex
29152917
if new_values.size and isinstance(new_values[0], tuple):
29162918
if isinstance(self, MultiIndex):
29172919
names = self.names
@@ -2932,6 +2934,17 @@ def map(self, mapper, na_action=None):
29322934
if inferred == 'integer':
29332935
attributes['dtype'] = self.dtype
29342936

2937+
elif not new_values.size:
2938+
# empty
2939+
attributes['dtype'] = self.dtype
2940+
elif isna(new_values).all():
2941+
# all nan
2942+
inferred = lib.infer_dtype(self)
2943+
if inferred in ['datetime', 'datetime64',
2944+
'timedelta', 'timedelta64',
2945+
'period']:
2946+
new_values = [libts.NaT] * len(new_values)
2947+
29352948
return Index(new_values, **attributes)
29362949

29372950
def isin(self, values, level=None):

pandas/tests/indexes/datetimelike.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
""" generic datetimelike tests """
22
import pytest
33
import pandas as pd
4-
import numpy as np
54
from .common import Base
65
import pandas.util.testing as tm
76

@@ -73,6 +72,6 @@ def test_map_dictlike(self, mapper):
7372

7473
# empty map; these map to np.nan because we cannot know
7574
# to re-infer things
76-
expected = pd.Index([np.nan] * len(self.index))
75+
expected = pd.Index([pd.NaT] * len(self.index))
7776
result = self.index.map(mapper([], []))
7877
tm.assert_index_equal(result, expected)

pandas/tests/indexes/test_base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ def test_map_dictlike(self, mapper):
878878
if name == 'uintIndex':
879879
expected = expected.astype('uint64')
880880
elif name == 'empty':
881-
expected = Float64Index([])
881+
expected = Index([])
882882

883883
result = index.map(mapper(expected, index))
884884
tm.assert_index_equal(result, expected)

0 commit comments

Comments
 (0)