-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
[Feature request] Add a "filter" method to Index and Series #27439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
.loc can already accept a callable how is this different? |
Am I using the callabale feature incorrectly? series.loc[func]
# ---------------------------------------------------------------------------
# ValueError Traceback (most recent call last)
# <ipython-input-2-da6f788021a1> in <module>
# ----> 1 series.loc[func]
# ~/anaconda/envs/µ_env/lib/python3.6/site-packages/pandas/core/indexing.py in __getitem__(self, key)
# 1497 axis = self.axis or 0
# 1498
# -> 1499 maybe_callable = com.apply_if_callable(key, self.obj)
# 1500 return self._getitem_axis(maybe_callable, axis=axis)
# 1501
# ~/anaconda/envs/µ_env/lib/python3.6/site-packages/pandas/core/common.py in apply_if_callable(maybe_callable, obj, **kwargs)
# 327
# 328 if callable(maybe_callable):
# --> 329 return maybe_callable(obj, **kwargs)
# 330
# 331 return maybe_callable
# <ipython-input-1-308c816fe0c4> in <lambda>(x)
# 8
# 9 # New way
# ---> 10 func = lambda x:x in ["o", "_"]
# 11 print("New way:", series.filter(func))
# 12
# ~/anaconda/envs/µ_env/lib/python3.6/site-packages/pandas/core/generic.py in __nonzero__(self)
# 1476 raise ValueError("The truth value of a {0} is ambiguous. "
# 1477 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
# -> 1478 .format(self.__class__.__name__))
# 1479
# 1480 __bool__ = __nonzero__
# ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
# |
from https://pandas.pydata.org/docs/reference/api/pandas.Series.loc.html
to use something like
consistency would be ideal but unfortunately Series already has a see also #26642 and linked issues for discussion on current |
Code Sample, a copy-pastable example if possible
Problem description
II always find myself writing really verbose code to filter my pandas indices or series. I love the map method and would like to extend this concept to include
filter
.The text was updated successfully, but these errors were encountered: