diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 5fcb5f09dfae7..4b1cb9c20e465 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -690,7 +690,7 @@ def astype_nansafe(arr, dtype, copy=True): raise ValueError('Cannot convert non-finite values (NA or inf) to ' 'integer') - elif arr.dtype == np.object_ and np.issubdtype(dtype.type, np.integer): + elif is_object_dtype(arr.dtype) and np.issubdtype(dtype.type, np.integer): # work around NumPy brokenness, #1987 return lib.astype_intsafe(arr.ravel(), dtype).reshape(arr.shape) @@ -703,6 +703,19 @@ def astype_nansafe(arr, dtype, copy=True): dtype = np.dtype(dtype.name + "[ns]") if copy: + + if arr.dtype == dtype: + return arr.copy() + + # we handle datetimelikes with pandas machinery + # to be robust to the input type + elif is_datetime64_dtype(dtype): + from pandas import to_datetime + return to_datetime(arr).values + elif is_timedelta64_dtype(dtype): + from pandas import to_timedelta + return to_timedelta(arr).values + return arr.astype(dtype) return arr.view(dtype) diff --git a/pandas/tests/io/json/test_json_table_schema.py b/pandas/tests/io/json/test_json_table_schema.py index fc3790287d7da..ccccdc9b0863e 100644 --- a/pandas/tests/io/json/test_json_table_schema.py +++ b/pandas/tests/io/json/test_json_table_schema.py @@ -499,7 +499,7 @@ def test_read_json_table_orient(self, index_nm, vals): tm.assert_frame_equal(df, result) @pytest.mark.parametrize("index_nm", [ - None, "idx", pytest.param("index", marks=pytest.mark.xfail)]) + None, "idx", "index"]) @pytest.mark.parametrize("vals", [ {'timedeltas': pd.timedelta_range('1H', periods=4, freq='T')}, {'timezones': pd.date_range('2016-01-01', freq='d', periods=4,