Skip to content

Commit 05f7ef9

Browse files
authored
BUG: Fix ListAccessor methods to preserve original name (pandas-dev#60527)
* fix: preserve series name in ListAccessor * formatting * add whatsnew v3.0.0 entry
1 parent 59f947f commit 05f7ef9

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,7 @@ Other
798798
- Bug in :meth:`read_csv` where chained fsspec TAR file and ``compression="infer"`` fails with ``tarfile.ReadError`` (:issue:`60028`)
799799
- Bug in Dataframe Interchange Protocol implementation was returning incorrect results for data buffers' associated dtype, for string and datetime columns (:issue:`54781`)
800800
- Bug in ``Series.list`` methods not preserving the original :class:`Index`. (:issue:`58425`)
801+
- Bug in ``Series.list`` methods not preserving the original name. (:issue:`60522`)
801802
- Bug in printing a :class:`DataFrame` with a :class:`DataFrame` stored in :attr:`DataFrame.attrs` raised a ``ValueError`` (:issue:`60455`)
802803

803804
.. ***DO NOT USE THIS SECTION***

pandas/core/arrays/arrow/accessors.py

+20-4
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ def len(self) -> Series:
117117

118118
value_lengths = pc.list_value_length(self._pa_array)
119119
return Series(
120-
value_lengths, dtype=ArrowDtype(value_lengths.type), index=self._data.index
120+
value_lengths,
121+
dtype=ArrowDtype(value_lengths.type),
122+
index=self._data.index,
123+
name=self._data.name,
121124
)
122125

123126
def __getitem__(self, key: int | slice) -> Series:
@@ -162,7 +165,10 @@ def __getitem__(self, key: int | slice) -> Series:
162165
# key = pc.add(key, pc.list_value_length(self._pa_array))
163166
element = pc.list_element(self._pa_array, key)
164167
return Series(
165-
element, dtype=ArrowDtype(element.type), index=self._data.index
168+
element,
169+
dtype=ArrowDtype(element.type),
170+
index=self._data.index,
171+
name=self._data.name,
166172
)
167173
elif isinstance(key, slice):
168174
if pa_version_under11p0:
@@ -181,7 +187,12 @@ def __getitem__(self, key: int | slice) -> Series:
181187
if step is None:
182188
step = 1
183189
sliced = pc.list_slice(self._pa_array, start, stop, step)
184-
return Series(sliced, dtype=ArrowDtype(sliced.type), index=self._data.index)
190+
return Series(
191+
sliced,
192+
dtype=ArrowDtype(sliced.type),
193+
index=self._data.index,
194+
name=self._data.name,
195+
)
185196
else:
186197
raise ValueError(f"key must be an int or slice, got {type(key).__name__}")
187198

@@ -223,7 +234,12 @@ def flatten(self) -> Series:
223234
counts = pa.compute.list_value_length(self._pa_array)
224235
flattened = pa.compute.list_flatten(self._pa_array)
225236
index = self._data.index.repeat(counts.fill_null(pa.scalar(0, counts.type)))
226-
return Series(flattened, dtype=ArrowDtype(flattened.type), index=index)
237+
return Series(
238+
flattened,
239+
dtype=ArrowDtype(flattened.type),
240+
index=index,
241+
name=self._data.name,
242+
)
227243

228244

229245
class StructAccessor(ArrowAccessor):

pandas/tests/series/accessors/test_list_accessor.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ def test_list_getitem(list_dtype):
2525
ser = Series(
2626
[[1, 2, 3], [4, None, 5], None],
2727
dtype=ArrowDtype(list_dtype),
28+
name="a",
2829
)
2930
actual = ser.list[1]
30-
expected = Series([2, None, None], dtype="int64[pyarrow]")
31+
expected = Series([2, None, None], dtype="int64[pyarrow]", name="a")
3132
tm.assert_series_equal(actual, expected)
3233

3334

@@ -37,9 +38,15 @@ def test_list_getitem_index():
3738
[[1, 2, 3], [4, None, 5], None],
3839
dtype=ArrowDtype(pa.list_(pa.int64())),
3940
index=[1, 3, 7],
41+
name="a",
4042
)
4143
actual = ser.list[1]
42-
expected = Series([2, None, None], dtype="int64[pyarrow]", index=[1, 3, 7])
44+
expected = Series(
45+
[2, None, None],
46+
dtype="int64[pyarrow]",
47+
index=[1, 3, 7],
48+
name="a",
49+
)
4350
tm.assert_series_equal(actual, expected)
4451

4552

@@ -48,6 +55,7 @@ def test_list_getitem_slice():
4855
[[1, 2, 3], [4, None, 5], None],
4956
dtype=ArrowDtype(pa.list_(pa.int64())),
5057
index=[1, 3, 7],
58+
name="a",
5159
)
5260
if pa_version_under11p0:
5361
with pytest.raises(
@@ -60,6 +68,7 @@ def test_list_getitem_slice():
6068
[[2, 3], [None, 5], None],
6169
dtype=ArrowDtype(pa.list_(pa.int64())),
6270
index=[1, 3, 7],
71+
name="a",
6372
)
6473
tm.assert_series_equal(actual, expected)
6574

@@ -68,22 +77,25 @@ def test_list_len():
6877
ser = Series(
6978
[[1, 2, 3], [4, None], None],
7079
dtype=ArrowDtype(pa.list_(pa.int64())),
80+
name="a",
7181
)
7282
actual = ser.list.len()
73-
expected = Series([3, 2, None], dtype=ArrowDtype(pa.int32()))
83+
expected = Series([3, 2, None], dtype=ArrowDtype(pa.int32()), name="a")
7484
tm.assert_series_equal(actual, expected)
7585

7686

7787
def test_list_flatten():
7888
ser = Series(
7989
[[1, 2, 3], None, [4, None], [], [7, 8]],
8090
dtype=ArrowDtype(pa.list_(pa.int64())),
91+
name="a",
8192
)
8293
actual = ser.list.flatten()
8394
expected = Series(
8495
[1, 2, 3, 4, None, 7, 8],
8596
dtype=ArrowDtype(pa.int64()),
8697
index=[0, 0, 0, 2, 2, 4, 4],
98+
name="a",
8799
)
88100
tm.assert_series_equal(actual, expected)
89101

0 commit comments

Comments
 (0)