Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/json/test_json_table_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down