Skip to content

Commit

Permalink
[data] use iloc to access pandas Series (ray-project#42890)
Browse files Browse the repository at this point in the history
Signed-off-by: Hao Chen <[email protected]>
  • Loading branch information
raulchen authored Feb 2, 2024
1 parent f4d1900 commit 2b73e05
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions python/ray/data/_internal/pandas_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class PandasRow(TableRow):
def __getitem__(self, key: Union[str, List[str]]) -> Any:
from ray.data.extensions import TensorArrayElement

pd = lazy_import_pandas()

def get_item(keys: List[str]) -> Any:
col = self._row[keys]
if len(col) == 0:
Expand All @@ -67,12 +69,12 @@ def get_item(keys: List[str]) -> Any:
if isinstance(items[0], TensorArrayElement):
# Getting an item in a Pandas tensor column may return
# a TensorArrayElement, which we have to convert to an ndarray.
return tuple([item.to_numpy() for item in items])
return pd.Series(item.to_numpy() for item in items)

try:
# Try to interpret this as a numpy-type value.
# See https://stackoverflow.com/questions/9452775/converting-numpy-dtypes-to-native-python-types. # noqa: E501
return tuple([item.as_py() for item in items])
return pd.Series(item.as_py() for item in items)

except (AttributeError, ValueError):
# Fallback to the original form.
Expand All @@ -86,7 +88,7 @@ def get_item(keys: List[str]) -> Any:
if items is None:
return None
elif is_single_item:
return items[0]
return items.iloc[0]
else:
return items

Expand Down

0 comments on commit 2b73e05

Please sign in to comment.