Skip to content

Commit 3fca810

Browse files
committed
Implement UInt64 handling, tests, and docs
1 parent 04efd45 commit 3fca810

File tree

6 files changed

+35
-3
lines changed

6 files changed

+35
-3
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,7 @@ Datetimelike
13241324
- Bug in :func:`to_datetime` where ``box`` and ``utc`` arguments were ignored when passing a :class:`DataFrame` or ``dict`` of unit mappings (:issue:`23760`)
13251325
- Bug in :attr:`Series.dt` where the cache would not update properly after an in-place operation (:issue:`24408`)
13261326
- Bug in :class:`PeriodIndex` where comparisons against an array-like object with length 1 failed to raise ``ValueError`` (:issue:`23078`)
1327+
- Bug in :meth:`DatetimeIndex.astype`, :meth:`PeriodIndex.astype` and :meth:`TimedeltaIndex.astype` ignoring the sign of the ``dtype`` for unsigned integer dtypes (:issue:`24405`).
13271328

13281329
Timedelta
13291330
^^^^^^^^^

pandas/core/arrays/datetimelike.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
is_datetime64_dtype, is_datetime64tz_dtype, is_datetime_or_timedelta_dtype,
2222
is_dtype_equal, is_extension_array_dtype, is_float_dtype, is_integer_dtype,
2323
is_list_like, is_object_dtype, is_offsetlike, is_period_dtype,
24-
is_string_dtype, is_timedelta64_dtype, needs_i8_conversion, pandas_dtype)
24+
is_string_dtype, is_timedelta64_dtype, is_unsigned_integer_dtype,
25+
needs_i8_conversion, pandas_dtype)
2526
from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries
2627
from pandas.core.dtypes.missing import isna
2728

@@ -419,6 +420,11 @@ def astype(self, dtype, copy=True):
419420
# we deliberately ignore int32 vs. int64 here.
420421
# See https://github.com/pandas-dev/pandas/issues/24381 for more.
421422
values = self.asi8
423+
424+
if is_unsigned_integer_dtype(dtype):
425+
# Again, we ignore int32 vs. int64
426+
values = values.view("uint64")
427+
422428
if copy:
423429
values = values.copy()
424430
return values

pandas/core/indexes/base.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -739,8 +739,9 @@ def view(self, cls=None):
739739
Parameters
740740
----------
741741
dtype : numpy dtype or pandas type
742-
Note that any integer `dtype` is treated as ``'int64'``,
743-
regardless of the sign and size.
742+
Note that any signed integer `dtype` is treated as ``'int64'``,
743+
and any unsigned integer `dtype` is treated as ``'uint64'``,
744+
regardless of the size.
744745
copy : bool, default True
745746
By default, astype always returns a newly allocated object.
746747
If copy is set to False and internal requirements on dtype are

pandas/tests/indexes/datetimes/test_astype.py

+9
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ def test_astype(self):
3333
tm.assert_index_equal(result, Index(rng.asi8))
3434
tm.assert_numpy_array_equal(result.values, rng.asi8)
3535

36+
def test_astype_uint(self):
37+
arr = date_range('2000', periods=2)
38+
expected = pd.UInt64Index(
39+
np.array([946684800000000000, 946771200000000000], dtype="uint64")
40+
)
41+
42+
tm.assert_index_equal(arr.astype("uint64"), expected)
43+
tm.assert_index_equal(arr.astype("uint32"), expected)
44+
3645
def test_astype_with_tz(self):
3746

3847
# with tz

pandas/tests/indexes/period/test_astype.py

+6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ def test_astype_conversion(self):
4141
tm.assert_index_equal(result, Index(idx.asi8))
4242
tm.assert_numpy_array_equal(result.values, idx.asi8)
4343

44+
def test_astype_uint(self):
45+
arr = period_range('2000', periods=2)
46+
expected = pd.UInt64Index(np.array([10957, 10958], dtype='uint64'))
47+
tm.assert_index_equal(arr.astype("uint64"), expected)
48+
tm.assert_index_equal(arr.astype("uint32"), expected)
49+
4450
def test_astype_object(self):
4551
idx = pd.PeriodIndex([], freq='M')
4652

pandas/tests/indexes/timedeltas/test_astype.py

+9
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ def test_astype(self):
5454
tm.assert_index_equal(result, Index(rng.asi8))
5555
tm.assert_numpy_array_equal(rng.asi8, result.values)
5656

57+
def test_astype_uint(self):
58+
arr = timedelta_range('1H', periods=2)
59+
expected = pd.UInt64Index(
60+
np.array([3600000000000, 90000000000000], dtype="uint64")
61+
)
62+
63+
tm.assert_index_equal(arr.astype("uint64"), expected)
64+
tm.assert_index_equal(arr.astype("uint32"), expected)
65+
5766
def test_astype_timedelta64(self):
5867
# GH 13149, GH 13209
5968
idx = TimedeltaIndex([1e14, 'NaT', NaT, np.NaN])

0 commit comments

Comments
 (0)