Skip to content

Commit 509e03c

Browse files
mattayesjreback
authored andcommitted
BUG: pandas-dev#12815 Always use np.nan for missing values of object dtypes (pandas-dev#18313)
1 parent 8512cc5 commit 509e03c

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

doc/source/whatsnew/v0.22.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Other API Changes
5757
- :class:`CacheableOffset` and :class:`WeekDay` are no longer available in the ``pandas.tseries.offsets`` module (:issue:`17830`)
5858
- `tseries.frequencies.get_freq_group()` and `tseries.frequencies.DAYS` are removed from the public API (:issue:`18034`)
5959
- :func:`Series.truncate` and :func:`DataFrame.truncate` will raise a ``ValueError`` if the index is not sorted instead of an unhelpful ``KeyError`` (:issue:`17935`)
60+
- :func:`Dataframe.unstack` will now default to filling with ``np.nan`` for ``object`` columns. (:issue:`12815`)
6061

6162

6263
.. _whatsnew_0220.deprecations:

pandas/core/dtypes/cast.py

+1
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ def maybe_promote(dtype, fill_value=np.nan):
322322
fill_value = iNaT
323323
else:
324324
dtype = np.object_
325+
fill_value = np.nan
325326
else:
326327
dtype = np.object_
327328

pandas/tests/frame/test_reshape.py

+23
Original file line numberDiff line numberDiff line change
@@ -783,3 +783,26 @@ def test_stack_preserve_categorical_dtype(self):
783783
expected = Series([10, 11, 12], index=midx)
784784

785785
tm.assert_series_equal(result, expected)
786+
787+
788+
def test_unstack_fill_frame_object():
789+
# GH12815 Test unstacking with object.
790+
data = pd.Series(['a', 'b', 'c', 'a'], dtype='object')
791+
data.index = pd.MultiIndex.from_tuples(
792+
[('x', 'a'), ('x', 'b'), ('y', 'b'), ('z', 'a')])
793+
794+
# By default missing values will be NaN
795+
result = data.unstack()
796+
expected = pd.DataFrame(
797+
{'a': ['a', np.nan, 'a'], 'b': ['b', 'c', np.nan]},
798+
index=list('xyz')
799+
)
800+
assert_frame_equal(result, expected)
801+
802+
# Fill with any value replaces missing values as expected
803+
result = data.unstack(fill_value='d')
804+
expected = pd.DataFrame(
805+
{'a': ['a', 'd', 'a'], 'b': ['b', 'c', 'd']},
806+
index=list('xyz')
807+
)
808+
assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)