Skip to content

Commit 245a89c

Browse files
authored
feat: support series items method (#1089)
* feat: series items method * fix lint format * use to_pandas_batches * fix doctest
1 parent e8d689e commit 245a89c

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

bigframes/series.py

+8
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,14 @@ def ne(self, other: object) -> Series:
11171117
# TODO: enforce stricter alignment
11181118
return self._apply_binary_op(other, ops.ne_op)
11191119

1120+
def items(self):
1121+
for batch_df in self._block.to_pandas_batches():
1122+
assert (
1123+
batch_df.shape[1] == 1
1124+
), f"Expected 1 column in the dataframe, but got {batch_df.shape[1]}."
1125+
for item in batch_df.squeeze(axis=1).items():
1126+
yield item
1127+
11201128
def where(self, cond, other=None):
11211129
value_id, cond_id, other_id, block = self._align3(cond, other)
11221130
block, result_id = block.project_expr(

tests/system/small/test_series.py

+24
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,30 @@ def test_series_construct_geodata():
227227
)
228228

229229

230+
@pytest.mark.parametrize(
231+
["data", "index"],
232+
[
233+
(["a", "b", "c"], None),
234+
([1, 2, 3], ["a", "b", "c"]),
235+
([1, 2, None], ["a", "b", "c"]),
236+
([1, 2, 3], [pd.NA, "b", "c"]),
237+
([numpy.nan, 2, 3], ["a", "b", "c"]),
238+
],
239+
)
240+
def test_series_items(data, index):
241+
bf_series = series.Series(data, index=index)
242+
pd_series = pd.Series(data, index=index)
243+
244+
for (bf_index, bf_value), (pd_index, pd_value) in zip(
245+
bf_series.items(), pd_series.items()
246+
):
247+
# TODO(jialuo): Remove the if conditions after b/373699458 is addressed.
248+
if not pd.isna(bf_index) or not pd.isna(pd_index):
249+
assert bf_index == pd_index
250+
if not pd.isna(bf_value) or not pd.isna(pd_value):
251+
assert bf_value == pd_value
252+
253+
230254
@pytest.mark.parametrize(
231255
["col_name", "expected_dtype"],
232256
[

third_party/bigframes_vendored/pandas/core/series.py

+36
Original file line numberDiff line numberDiff line change
@@ -3332,6 +3332,42 @@ def kurt(self):
33323332
"""
33333333
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)
33343334

3335+
def items(self):
3336+
"""
3337+
Iterate over (index, value) pairs of a Series.
3338+
3339+
Iterates over the Series contents, returning a tuple with
3340+
the index and the value of a Series.
3341+
3342+
**Examples:**
3343+
3344+
>>> import bigframes.pandas as bpd
3345+
>>> bpd.options.display.progress_bar = None
3346+
3347+
>>> s = bpd.Series(['bear', 'bear', 'marsupial'],
3348+
... index=['panda', 'polar', 'koala'])
3349+
>>> s
3350+
panda bear
3351+
polar bear
3352+
koala marsupial
3353+
dtype: string
3354+
3355+
>>> for index, value in s.items():
3356+
... print(f'--> index: {index}')
3357+
... print(f'--> value: {value}')
3358+
...
3359+
--> index: panda
3360+
--> value: bear
3361+
--> index: polar
3362+
--> value: bear
3363+
--> index: koala
3364+
--> value: marsupial
3365+
3366+
Returns:
3367+
Iterator: Iterator of index, value for each content of the Series.
3368+
"""
3369+
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)
3370+
33353371
def where(self, cond, other):
33363372
"""Replace values where the condition is False.
33373373

0 commit comments

Comments
 (0)