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 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
7 changes: 5 additions & 2 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,11 @@ if [[ -z "$CHECK" || "$CHECK" == "doctests" ]]; then
-k"-from_arrays -from_breaks -from_intervals -from_tuples -set_closed -to_tuples -interval_range"
RET=$(($RET + $?)) ; echo $MSG "DONE"

MSG='Doctests arrays/string_.py' ; echo $MSG
pytest -q --doctest-modules pandas/core/arrays/string_.py
MSG='Doctests arrays'; echo $MSG
pytest -q --doctest-modules \
pandas/core/arrays/string_.py \
pandas/core/arrays/integer.py \
pandas/core/arrays/boolean.py
RET=$(($RET + $?)) ; echo $MSG "DONE"

MSG='Doctests arrays/boolean.py' ; echo $MSG
Expand Down
2 changes: 1 addition & 1 deletion doc/source/user_guide/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1153,7 +1153,7 @@ To completely override the default values that are recognized as missing, specif
.. _io.navaluesconst:

The default ``NaN`` recognized values are ``['-1.#IND', '1.#QNAN', '1.#IND', '-1.#QNAN', '#N/A N/A', '#N/A', 'N/A',
'n/a', 'NA', '#NA', 'NULL', 'null', 'NaN', '-NaN', 'nan', '-nan', '']``.
'n/a', 'NA', '<NA>', '#NA', 'NULL', 'null', 'NaN', '-NaN', 'nan', '-nan', '']``.

Let us consider some examples:

Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ Other API changes
Supplying anything else than ``how`` to ``**kwargs`` raised a ``TypeError`` previously (:issue:`29388`)
- When testing pandas, the new minimum required version of pytest is 5.0.1 (:issue:`29664`)
- :meth:`Series.str.__iter__` was deprecated and will be removed in future releases (:issue:`28277`).
- Added ``<NA>`` to the list of default NA values for :meth:`read_csv` (:issue:`30821`)
Copy link
Contributor

Choose a reason for hiding this comment

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

may want to also mention in the NA section



.. _whatsnew_100.api.documentation:
Expand Down
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
1 change: 1 addition & 0 deletions pandas/_libs/parsers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,7 @@ STR_NA_VALUES = {
"N/A",
"n/a",
"NA",
"<NA>",
"#NA",
"NULL",
"null",
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/arrays/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class BooleanArray(BaseMaskedArray):

>>> pd.array([True, False, None], dtype="boolean")
<BooleanArray>
[True, False, NA]
[True, False, <NA>]
Length: 3, dtype: boolean
"""

Expand Down Expand Up @@ -527,7 +527,7 @@ def any(self, skipna: bool = True, **kwargs):
>>> pd.array([True, False, pd.NA]).any(skipna=False)
True
>>> pd.array([False, False, pd.NA]).any(skipna=False)
NA
<NA>
"""
kwargs.pop("axis", None)
nv.validate_any((), kwargs)
Expand Down Expand Up @@ -592,7 +592,7 @@ def all(self, skipna: bool = True, **kwargs):
required (whether ``pd.NA`` is True or False influences the result):

>>> pd.array([True, True, pd.NA]).all(skipna=False)
NA
<NA>
>>> pd.array([True, False, pd.NA]).all(skipna=False)
False
"""
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,19 +301,19 @@ class IntegerArray(BaseMaskedArray):
>>> int_array = pd.array([1, None, 3], dtype=pd.Int32Dtype())
>>> int_array
<IntegerArray>
[1, NaN, 3]
[1, <NA>, 3]
Length: 3, dtype: Int32

String aliases for the dtypes are also available. They are capitalized.

>>> pd.array([1, None, 3], dtype='Int32')
<IntegerArray>
[1, NaN, 3]
[1, <NA>, 3]
Length: 3, dtype: Int32

>>> pd.array([1, None, 3], dtype='UInt16')
<IntegerArray>
[1, NaN, 3]
[1, <NA>, 3]
Length: 3, dtype: UInt16
"""

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/string_.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class StringArray(PandasArray):
--------
>>> pd.array(['This is', 'some text', None, 'data.'], dtype="string")
<StringArray>
['This is', 'some text', NA, 'data.']
['This is', 'some text', <NA>, 'data.']
Length: 4, dtype: string

Unlike ``object`` dtype arrays, ``StringArray`` doesn't allow non-string
Expand All @@ -146,7 +146,7 @@ class StringArray(PandasArray):

>>> pd.array(["a", None, "c"], dtype="string") == "a"
<BooleanArray>
[True, NA, False]
[True, <NA>, False]
Length: 3, dtype: boolean
"""

Expand Down
8 changes: 2 additions & 6 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1777,12 +1777,8 @@ def to_native_types(self, slicer=None, na_rep="nan", quoting=None, **kwargs):
values = values[slicer]
mask = isna(values)

try:
values[mask] = na_rep
except Exception:
# eg SparseArray does not support setitem, needs to be converted to ndarray
return super().to_native_types(slicer, na_rep, quoting, **kwargs)
values = values.astype(str)
values = np.asarray(values.astype(object))
values[mask] = na_rep

# we are expected to return a 2-d ndarray
return values.reshape(1, len(values))
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
18 changes: 10 additions & 8 deletions pandas/tests/arrays/string_/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
import pandas._testing as tm


def test_repr_with_NA():
a = pd.array(["a", pd.NA, "b"], dtype="string")
for obj in [a, pd.Series(a), pd.DataFrame({"a": a})]:
assert "NA" in repr(obj) and "NaN" not in repr(obj)
assert "NA" in str(obj) and "NaN" not in str(obj)
if hasattr(obj, "_repr_html_"):
html_repr = obj._repr_html_()
assert "NA" in html_repr and "NaN" not in html_repr
def test_repr():
df = pd.DataFrame({"A": pd.array(["a", pd.NA, "b"], dtype="string")})
expected = " A\n0 a\n1 <NA>\n2 b"
assert repr(df) == expected

expected = "0 a\n1 <NA>\n2 b\nName: A, dtype: string"
assert repr(df.A) == expected

expected = "<StringArray>\n['a', <NA>, 'b']\nLength: 3, dtype: string"
assert repr(df.A.array) == expected


def test_none_to_nan():
Expand Down
14 changes: 13 additions & 1 deletion pandas/tests/arrays/test_boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,18 @@ def test_coerce_to_numpy_array():
np.array(arr, dtype="bool")


def test_repr():
df = pd.DataFrame({"A": pd.array([True, False, None], dtype="boolean")})
expected = " A\n0 True\n1 False\n2 <NA>"
assert repr(df) == expected

expected = "0 True\n1 False\n2 <NA>\nName: A, dtype: boolean"
assert repr(df.A) == expected

expected = "<BooleanArray>\n[True, False, <NA>]\nLength: 3, dtype: boolean"
assert repr(df.A.array) == expected


@pytest.mark.parametrize("box", [True, False], ids=["series", "array"])
def test_to_numpy(box):
con = pd.Series if box else pd.array
Expand Down Expand Up @@ -335,7 +347,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
1 change: 1 addition & 0 deletions pandas/tests/io/parser/test_na_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def test_default_na_values(all_parsers):
"N/A",
"n/a",
"NA",
"<NA>",
"#NA",
"NULL",
"null",
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/scalar/test_na_scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def test_singleton():


def test_repr():
assert repr(NA) == "NA"
assert str(NA) == "NA"
assert repr(NA) == "<NA>"
assert str(NA) == "<NA>"


def test_truthiness():
Expand Down