Skip to content

Commit 7483ee9

Browse files
jbrockmendelnickleus27
authored andcommitted
BUG: DataFrame with mismatched NA value and dtype (pandas-dev#44428)
1 parent c456969 commit 7483ee9

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@ Missing
608608
^^^^^^^
609609
- Bug in :meth:`DataFrame.fillna` with limit and no method ignores axis='columns' or ``axis = 1`` (:issue:`40989`)
610610
- Bug in :meth:`DataFrame.fillna` not replacing missing values when using a dict-like ``value`` and duplicate column names (:issue:`43476`)
611+
- Bug in constructing a :class:`DataFrame` with a dictionary ``np.datetime64`` as a value and ``dtype='timedelta64[ns]'``, or vice-versa, incorrectly casting instead of raising (:issue:`??`)
611612
-
612613

613614
MultiIndex

pandas/core/internals/construction.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -443,15 +443,18 @@ def dict_to_mgr(
443443
if missing.any() and not is_integer_dtype(dtype):
444444
nan_dtype: DtypeObj
445445

446-
if dtype is None or (
447-
isinstance(dtype, np.dtype) and np.issubdtype(dtype, np.flexible)
448-
):
446+
if dtype is not None:
447+
# calling sanitize_array ensures we don't mix-and-match
448+
# NA dtypes
449+
midxs = missing.values.nonzero()[0]
450+
for i in midxs:
451+
arr = sanitize_array(arrays.iat[i], index, dtype=dtype)
452+
arrays.iat[i] = arr
453+
else:
449454
# GH#1783
450455
nan_dtype = np.dtype("object")
451-
else:
452-
nan_dtype = dtype
453-
val = construct_1d_arraylike_from_scalar(np.nan, len(index), nan_dtype)
454-
arrays.loc[missing] = [val] * missing.sum()
456+
val = construct_1d_arraylike_from_scalar(np.nan, len(index), nan_dtype)
457+
arrays.loc[missing] = [val] * missing.sum()
455458

456459
arrays = list(arrays)
457460
columns = ensure_index(columns)

pandas/tests/frame/test_constructors.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -2903,14 +2903,7 @@ def test_from_timedelta64_scalar_object(self, constructor):
29032903
assert isinstance(get1(obj), np.timedelta64)
29042904

29052905
@pytest.mark.parametrize("cls", [np.datetime64, np.timedelta64])
2906-
def test_from_scalar_datetimelike_mismatched(self, constructor, cls, request):
2907-
node = request.node
2908-
params = node.callspec.params
2909-
if params["frame_or_series"] is DataFrame and params["constructor"] is dict:
2910-
mark = pytest.mark.xfail(
2911-
reason="DataFrame incorrectly allows mismatched datetimelike"
2912-
)
2913-
node.add_marker(mark)
2906+
def test_from_scalar_datetimelike_mismatched(self, constructor, cls):
29142907
scalar = cls("NaT", "ns")
29152908
dtype = {np.datetime64: "m8[ns]", np.timedelta64: "M8[ns]"}[cls]
29162909

0 commit comments

Comments
 (0)