Skip to content

Repr for Integer and Pandas Dtypes #24646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 5, 2019
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
5 changes: 5 additions & 0 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class _IntegerDtype(ExtensionDtype):
type = None
na_value = np.nan

def __repr__(self):
sign = 'U' if self.is_unsigned_integer else ''
return "{sign}Int{size}Dtype()".format(sign=sign,
size=8 * self.itemsize)

@cache_readonly
def is_signed_integer(self):
return self.kind == 'i'
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ def __init__(self, dtype):
self._name = dtype.name
self._type = dtype.type

def __repr__(self):
return "PandasDtype({!r})".format(self.name)

@property
def numpy_dtype(self):
"""The NumPy dtype this PandasDtype wraps."""
return self._dtype

@property
Expand Down Expand Up @@ -72,6 +76,7 @@ def kind(self):

@property
def itemsize(self):
"""The element size of this data-type object."""
return self._dtype.itemsize


Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/arrays/test_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ def test_dtypes(dtype):
assert dtype.name is not None


@pytest.mark.parametrize('dtype, expected', [
(Int8Dtype(), 'Int8Dtype()'),
(Int16Dtype(), 'Int16Dtype()'),
(Int32Dtype(), 'Int32Dtype()'),
(Int64Dtype(), 'Int64Dtype()'),
(UInt8Dtype(), 'UInt8Dtype()'),
(UInt16Dtype(), 'UInt16Dtype()'),
(UInt32Dtype(), 'UInt32Dtype()'),
(UInt64Dtype(), 'UInt64Dtype()'),
])
def test_repr_dtype(dtype, expected):
assert repr(dtype) == expected


def test_repr_array():
result = repr(integer_array([1, None, 3]))
expected = (
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/arrays/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ def test_is_boolean(dtype, expected):
assert dtype._is_boolean is expected


def test_repr():
dtype = PandasDtype(np.dtype("int64"))
assert repr(dtype) == "PandasDtype('int64')"


def test_constructor_from_string():
result = PandasDtype.construct_from_string("int64")
expected = PandasDtype(np.dtype("int64"))
assert result == expected


# ----------------------------------------------------------------------------
# Construction

Expand Down