Skip to content
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

ENH: add __rshift__, __lshift__, __irshift__ and __ilshift__ #60693

Open
1 of 3 tasks
luxedo opened this issue Jan 11, 2025 · 1 comment
Open
1 of 3 tasks

ENH: add __rshift__, __lshift__, __irshift__ and __ilshift__ #60693

luxedo opened this issue Jan 11, 2025 · 1 comment
Labels
Enhancement Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@luxedo
Copy link

luxedo commented Jan 11, 2025

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

Pandas has many in-place operators implemented:

__iadd__,  __isub__,  __imul__,  __itruediv_ __ifloordiv __imod__,  __ipow__,  __iand__,  __ixor__,  __ior__, __imatmul__

As for __rshift__, __lshift__, __irshift__ and __ilshift__ they don't work in version 2.2.2.

>>> import pandas as pd
>>> s = pd.Series([0, 1, 2])
>>> s <<= 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for <<=: 'Series' and 'int'

And numpy implements all methods:

>>> import numpy as np 
>>> s = np.array([0, 1, 2])
>>> s <<= 3
>>> s
array([ 0,  8, 16])

Feature Description

Allow for __rshift__, __lshift__, __irshift__ and __ilshift__ calls in Series and DataFrames

>>> import pandas as pd 
>>> s = pd.Series([0, 1, 2])
>>> s << 3
0    0
1    8
2    16
>>> s <<= 3
>>> s
0    0
1    8
2    16

Alternative Solutions

For now, the solution is to create a new Series operating over the underlying numpy array

>>> pd.Series(s.values << 3)
0     0
1     8
2    16
dtype: int64

Additional Context

It is curious that the last issue that talks about shift operations is from more than 10 years ago (#2337). Indeed it's quite a niche feature and fits more computer scientists rather than data scientists. Anyway, I thought about this feature more for completeness and consistency with python operators rather than usefulness.

@luxedo luxedo added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 11, 2025
@luxedo
Copy link
Author

luxedo commented Jan 11, 2025

If that's a desired feature I'd be glad to add a PR for it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

1 participant