mirror of
https://github.com/wassname/ray.git
synced 2026-06-29 01:59:23 +08:00
[DataFrame] Implemented __getattr__ (#1753)
* __getattr__ accesses columns * Added test
This commit is contained in:
committed by
Devin Petersohn
parent
e82bea40b1
commit
405b05d58a
@@ -2992,6 +2992,22 @@ class DataFrame(object):
|
||||
lambda df: df.__getitem__(index),
|
||||
self._col_partitions[part])
|
||||
|
||||
def __getattr__(self, key):
|
||||
"""After regular attribute access, looks up the name in the columns
|
||||
|
||||
Args:
|
||||
key (str): Attribute name.
|
||||
|
||||
Returns:
|
||||
The value of the attribute.
|
||||
"""
|
||||
try:
|
||||
return object.__getattribute__(self, key)
|
||||
except AttributeError as e:
|
||||
if key in self.columns:
|
||||
return self[key]
|
||||
raise e
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
raise NotImplementedError(
|
||||
"To contribute to Pandas on Ray, please visit "
|
||||
|
||||
@@ -2763,6 +2763,23 @@ def test___getitem__(ray_df, pd_df):
|
||||
assert pd_col.equals(ray_col)
|
||||
|
||||
|
||||
def test___getattr__():
|
||||
df = create_test_dataframe()
|
||||
|
||||
col = df.__getattr__("col1")
|
||||
assert isinstance(col, pd.Series)
|
||||
|
||||
col = getattr(df, "col1")
|
||||
assert isinstance(col, pd.Series)
|
||||
|
||||
col = df.col1
|
||||
assert isinstance(col, pd.Series)
|
||||
|
||||
# Check that lookup in column doesn't override other attributes
|
||||
df2 = df.rename(index=str, columns={"col5": "columns"})
|
||||
assert isinstance(df2.columns, pd.Index)
|
||||
|
||||
|
||||
def test___setitem__():
|
||||
ray_df = create_test_dataframe()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user