Skip to content

Commit

Permalink
Merge branch 'main' into pandas.period_range
Browse files Browse the repository at this point in the history
  • Loading branch information
tuhinsharma121 authored Oct 5, 2024
2 parents f12fd9b + aea1643 commit 1fd46b0
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 34 deletions.
3 changes: 2 additions & 1 deletion doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ Bug fixes

Categorical
^^^^^^^^^^^
-
- Bug in :func:`Series.apply` where ``nan`` was ignored for :class:`CategoricalDtype` (:issue:`59938`)
-

Datetimelike
Expand Down Expand Up @@ -682,6 +682,7 @@ Sparse
^^^^^^
- Bug in :class:`SparseDtype` for equal comparison with na fill value. (:issue:`54770`)
- Bug in :meth:`DataFrame.sparse.from_spmatrix` which hard coded an invalid ``fill_value`` for certain subtypes. (:issue:`59063`)
- Bug in :meth:`DataFrame.sparse.to_dense` which ignored subclassing and always returned an instance of :class:`DataFrame` (:issue:`59913`)

ExtensionArray
^^^^^^^^^^^^^^
Expand Down
14 changes: 2 additions & 12 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@
is_numeric_dtype,
is_sequence,
)
from pandas.core.dtypes.dtypes import (
CategoricalDtype,
ExtensionDtype,
)
from pandas.core.dtypes.dtypes import ExtensionDtype
from pandas.core.dtypes.generic import (
ABCDataFrame,
ABCNDFrame,
Expand Down Expand Up @@ -1465,14 +1462,7 @@ def curried(x):

else:
curried = func

# row-wise access
# apply doesn't have a `na_action` keyword and for backward compat reasons
# we need to give `na_action="ignore"` for categorical data.
# TODO: remove the `na_action="ignore"` when that default has been changed in
# Categorical (GH51645).
action = "ignore" if isinstance(obj.dtype, CategoricalDtype) else None
mapped = obj._map_values(mapper=curried, na_action=action)
mapped = obj._map_values(mapper=curried)

if len(mapped) and isinstance(mapped[0], ABCSeries):
# GH#43986 Need to do list(mapped) in order to get treated as nested
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/arrays/sparse/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,10 @@ def to_dense(self) -> DataFrame:
1 1
2 0
"""
from pandas import DataFrame

data = {k: v.array.to_dense() for k, v in self._parent.items()}
return DataFrame(data, index=self._parent.index, columns=self._parent.columns)
return self._parent._constructor(
data, index=self._parent.index, columns=self._parent.columns
)

def to_coo(self) -> spmatrix:
"""
Expand Down
2 changes: 2 additions & 0 deletions pandas/io/formats/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ def _write_cell(
esc = {}

rs = pprint_thing(s, escape_chars=esc).strip()
# replace spaces betweens strings with non-breaking spaces
rs = rs.replace(" ", "  ")

if self.render_links and is_url(rs):
rs_unescaped = pprint_thing(s, escape_chars={}).strip()
Expand Down
24 changes: 12 additions & 12 deletions pandas/io/formats/style_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,9 +906,9 @@ def concatenated_visible_rows(obj):
row_body_headers = [
{
**col,
"display_value": col["display_value"]
if col["is_visible"]
else "",
"display_value": (
col["display_value"] if col["is_visible"] else ""
),
"cellstyle": self.ctx_index[r, c],
}
for c, col in enumerate(row[:index_levels])
Expand Down Expand Up @@ -2069,18 +2069,18 @@ def maybe_convert_css_to_tuples(style: CSSProperties) -> CSSList:
('border','1px solid red')]
"""
if isinstance(style, str):
s = style.split(";")
try:
return [
(x.split(":")[0].strip(), x.split(":")[1].strip())
for x in s
if x.strip() != ""
]
except IndexError as err:
if style and ":" not in style:
raise ValueError(
"Styles supplied as string must follow CSS rule formats, "
f"for example 'attr: val;'. '{style}' was given."
) from err
)
s = style.split(";")
return [
(x.split(":")[0].strip(), ":".join(x.split(":")[1:]).strip())
for x in s
if x.strip() != ""
]

return style


Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,8 +741,9 @@ def test_apply_category_equalness(val):

result = df.a.apply(lambda x: x == val)
expected = Series(
[np.nan if pd.isnull(x) else x == val for x in df_values], name="a"
[False if pd.isnull(x) else x == val for x in df_values], name="a"
)
# False since behavior of NaN for categorical dtype has been changed (GH 59966)
tm.assert_series_equal(result, expected)


Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/apply/test_series_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,10 @@ def test_apply_categorical_with_nan_values(series, by_row):
with pytest.raises(AttributeError, match=msg):
s.apply(lambda x: x.split("-")[0], by_row=by_row)
return

result = s.apply(lambda x: x.split("-")[0], by_row=by_row)
# NaN for cat dtype fixed in (GH 59966)
result = s.apply(lambda x: x.split("-")[0] if pd.notna(x) else False, by_row=by_row)
result = result.astype(object)
expected = Series(["1", "1", np.nan], dtype="category")
expected = Series(["1", "1", False], dtype="category")
expected = expected.astype(object)
tm.assert_series_equal(result, expected)

Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/arrays/sparse/test_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,7 @@ def test_with_column_named_sparse(self):
# https://github.com/pandas-dev/pandas/issues/30758
df = pd.DataFrame({"sparse": pd.arrays.SparseArray([1, 2])})
assert isinstance(df.sparse, pd.core.arrays.sparse.accessor.SparseFrameAccessor)

def test_subclassing(self):
df = tm.SubclassedDataFrame({"sparse": pd.arrays.SparseArray([1, 2])})
assert isinstance(df.sparse.to_dense(), tm.SubclassedDataFrame)
13 changes: 12 additions & 1 deletion pandas/tests/io/formats/style/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,8 +886,19 @@ def test_maybe_convert_css_to_tuples(self):
expected = []
assert maybe_convert_css_to_tuples("") == expected

# issue #59623
expected = [("a", "b"), ("c", "url('data:123')")]
assert maybe_convert_css_to_tuples("a:b;c: url('data:123');") == expected

# if no value, return attr and empty string
expected = [("a", ""), ("c", "")]
assert maybe_convert_css_to_tuples("a:;c: ") == expected

def test_maybe_convert_css_to_tuples_err(self):
msg = "Styles supplied as string must follow CSS rule formats"
msg = (
"Styles supplied as string must follow CSS rule formats, "
"for example 'attr: val;'. 'err' was given."
)
with pytest.raises(ValueError, match=msg):
maybe_convert_css_to_tuples("err")

Expand Down
19 changes: 18 additions & 1 deletion pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,12 +375,29 @@ def test_repr_min_rows(self):
(None, "{:.3f}", "None"),
("", "{:.2f}", ""),
(112345.6789, "{:6.3f}", "112345.679"),
("foo foo", None, "foo      foo"),
(" foo", None, "foo"),
(
"foo foo foo",
None,
"foo foo       foo",
), # odd no.of spaces
(
"foo foo foo",
None,
"foo foo    foo",
), # even no.of spaces
],
)
def test_repr_float_formatting_html_output(
self, data, format_option, expected_values
):
with option_context("display.float_format", format_option.format):
if format_option is not None:
with option_context("display.float_format", format_option.format):
df = DataFrame({"A": [data]})
html_output = df._repr_html_()
assert expected_values in html_output
else:
df = DataFrame({"A": [data]})
html_output = df._repr_html_()
assert expected_values in html_output
Expand Down

0 comments on commit 1fd46b0

Please sign in to comment.