Skip to content

Commit 5d63aca

Browse files
TomAugspurgerPingviinituutti
authored andcommitted
Repr for Integer and Pandas Dtypes (pandas-dev#24646)
>>> pd.PandasDtype("int64") PandasDtype('int64') >>> pd.Int32Dtype() Int32Dtype() >>> pd.UInt8Dtype() UInt8Dtype()
1 parent c2c87d2 commit 5d63aca

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

pandas/core/arrays/integer.py

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ class _IntegerDtype(ExtensionDtype):
3636
type = None
3737
na_value = np.nan
3838

39+
def __repr__(self):
40+
sign = 'U' if self.is_unsigned_integer else ''
41+
return "{sign}Int{size}Dtype()".format(sign=sign,
42+
size=8 * self.itemsize)
43+
3944
@cache_readonly
4045
def is_signed_integer(self):
4146
return self.kind == 'i'

pandas/core/arrays/numpy_.py

+5
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,12 @@ def __init__(self, dtype):
3838
self._name = dtype.name
3939
self._type = dtype.type
4040

41+
def __repr__(self):
42+
return "PandasDtype({!r})".format(self.name)
43+
4144
@property
4245
def numpy_dtype(self):
46+
"""The NumPy dtype this PandasDtype wraps."""
4347
return self._dtype
4448

4549
@property
@@ -72,6 +76,7 @@ def kind(self):
7276

7377
@property
7478
def itemsize(self):
79+
"""The element size of this data-type object."""
7580
return self._dtype.itemsize
7681

7782

pandas/tests/arrays/test_integer.py

+14
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@ def test_dtypes(dtype):
5757
assert dtype.name is not None
5858

5959

60+
@pytest.mark.parametrize('dtype, expected', [
61+
(Int8Dtype(), 'Int8Dtype()'),
62+
(Int16Dtype(), 'Int16Dtype()'),
63+
(Int32Dtype(), 'Int32Dtype()'),
64+
(Int64Dtype(), 'Int64Dtype()'),
65+
(UInt8Dtype(), 'UInt8Dtype()'),
66+
(UInt16Dtype(), 'UInt16Dtype()'),
67+
(UInt32Dtype(), 'UInt32Dtype()'),
68+
(UInt64Dtype(), 'UInt64Dtype()'),
69+
])
70+
def test_repr_dtype(dtype, expected):
71+
assert repr(dtype) == expected
72+
73+
6074
def test_repr_array():
6175
result = repr(integer_array([1, None, 3]))
6276
expected = (

pandas/tests/arrays/test_numpy.py

+11
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ def test_is_boolean(dtype, expected):
7171
assert dtype._is_boolean is expected
7272

7373

74+
def test_repr():
75+
dtype = PandasDtype(np.dtype("int64"))
76+
assert repr(dtype) == "PandasDtype('int64')"
77+
78+
79+
def test_constructor_from_string():
80+
result = PandasDtype.construct_from_string("int64")
81+
expected = PandasDtype(np.dtype("int64"))
82+
assert result == expected
83+
84+
7485
# ----------------------------------------------------------------------------
7586
# Construction
7687

0 commit comments

Comments
 (0)