Skip to content

Update NA repr #30821

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 9 commits into from
Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 1 addition & 4 deletions pandas/_libs/missing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,7 @@ class NAType(C_NAType):
return NAType._instance

def __repr__(self) -> str:
return "NA"

def __str__(self) -> str:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python will automatically use __repr__ if one isn't defined, so this was redundant.

return "NA"
return "<NA>"

def __bool__(self):
raise TypeError("boolean value of NA is ambiguous")
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,7 @@ def _format(x):
if x is None:
return "None"
elif x is NA:
return "NA"
return formatter(x)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means that the formatter function needs to be able to handle NAs, which now is maybe not the case?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do a PR for this, I think this breaks geopandas

elif x is NaT or np.isnat(x):
return "NaT"
except (TypeError, ValueError):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/test_boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def test_astype():
tm.assert_numpy_array_equal(result, expected)

result = arr.astype("str")
expected = np.array(["True", "False", "NA"], dtype="object")
expected = np.array(["True", "False", "<NA>"], dtype="object")
tm.assert_numpy_array_equal(result, expected)

# no missing values
Expand Down
10 changes: 5 additions & 5 deletions pandas/tests/arrays/test_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,17 @@ def test_repr_dtype(dtype, expected):

def test_repr_array():
result = repr(integer_array([1, None, 3]))
expected = "<IntegerArray>\n[1, NA, 3]\nLength: 3, dtype: Int64"
expected = "<IntegerArray>\n[1, <NA>, 3]\nLength: 3, dtype: Int64"
assert result == expected


def test_repr_array_long():
data = integer_array([1, 2, None] * 1000)
expected = (
"<IntegerArray>\n"
"[ 1, 2, NA, 1, 2, NA, 1, 2, NA, 1,\n"
"[ 1, 2, <NA>, 1, 2, <NA>, 1, 2, <NA>, 1,\n"
" ...\n"
" NA, 1, 2, NA, 1, 2, NA, 1, 2, NA]\n"
" <NA>, 1, 2, <NA>, 1, 2, <NA>, 1, 2, <NA>]\n"
"Length: 3000, dtype: Int64"
)
result = repr(data)
Expand Down Expand Up @@ -673,7 +673,7 @@ def test_to_numpy_na_raises(self, dtype):

def test_astype_str(self):
a = pd.array([1, 2, None], dtype="Int64")
expected = np.array(["1", "2", "NA"], dtype=object)
expected = np.array(["1", "2", "<NA>"], dtype=object)

tm.assert_numpy_array_equal(a.astype(str), expected)
tm.assert_numpy_array_equal(a.astype("str"), expected)
Expand All @@ -683,7 +683,7 @@ def test_frame_repr(data_missing):

df = pd.DataFrame({"A": data_missing})
result = repr(df)
expected = " A\n0 NA\n1 1"
expected = " A\n0 <NA>\n1 1"
assert result == expected


Expand Down