Skip to content

Unary plus missing for Series and DataFrame #16073

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

Closed
RhysU opened this issue Apr 20, 2017 · 3 comments · Fixed by #19297
Closed

Unary plus missing for Series and DataFrame #16073

RhysU opened this issue Apr 20, 2017 · 3 comments · Fixed by #19297
Labels
Compat pandas objects compatability with Numpy or Python functions Enhancement good first issue Numeric Operations Arithmetic, Comparison, and Logical operations
Milestone

Comments

@RhysU
Copy link

RhysU commented Apr 20, 2017

Code Sample

In [1]: import numpy as np

In [2]: import pandas as pd

In [3]: pd.__version__
Out[3]: '0.19.2.post+ts5'

In [4]: a = np.array([-1,0,1])  # As expected

In [5]: +a
Out[5]: array([-1,  0,  1])

In [6]: s = pd.Series(data=a)

In [7]: +s  # Unexpected
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-d1319861d847> in <module>()
----> 1 +s

TypeError: bad operand type for unary +: 'Series'

In [8]: d = pd.DataFrame(a)

In [9]: +d  # Unexpected
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-e4fd8df22471> in <module>()
----> 1 +d

TypeError: bad operand type for unary +: 'DataFrame'

In [10]: -a
Out[10]: array([ 1,  0, -1])

In [11]: -s
Out[11]: 
0    1
1    0
2   -1
dtype: int64

In [12]: -d
Out[12]: 
   0
0  1
1  0
2 -1

Problem description

Unary plus is well-defined on built-in types and Numpy ndarrays. It is not defined for Series or DataFrame. It's not ever necessary, but the asymmetry with the present unary minus is unexpected. It's also occasionally nice to use it to line up a positive expression against a negative expression. Likely adding methods implemented as an identity is sufficient.

Expected Output

In [7]: +s
Out[7]: 
0   -1
1    0
2   1
dtype: int64

In [9]: +d
Out[9]: 
   0
0  -1
1  0
2 1

Output of pd.show_versions()

In [13]: pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.5.2.final.0
python-bits: 64
OS: Linux
OS-release: 4.1.35-pv-ts2
machine: x86_64
processor:
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.19.2.post+ts5
nose: 1.3.7
pip: 9.0.1
setuptools: 27.2.0
Cython: 0.25.2
numpy: 1.11.3
scipy: 0.19.0
statsmodels: None
xarray: None
IPython: 5.1.0
sphinx: 1.5.1
patsy: None
dateutil: 2.6.0
pytz: 2016.10
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 1.5.1
openpyxl: 2.4.5
xlrd: 1.0.0
xlwt: None
xlsxwriter: None
lxml: None
bs4: 4.5.3
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.1.9
pymysql: None
psycopg2: None
jinja2: 2.9.4
boto: None
pandas_datareader: None

@RhysU
Copy link
Author

RhysU commented Apr 20, 2017

Obviously low priority.

@jreback
Copy link
Contributor

jreback commented Apr 20, 2017

sure this is pretty straightforward to add actually

see how we implement __neg__ https://github.com/pandas-dev/pandas/blob/master/pandas/core/generic.py#L862

a pull request would be great! (with tests!)

@jreback jreback added Compat pandas objects compatability with Numpy or Python functions Difficulty Novice Enhancement Numeric Operations Arithmetic, Comparison, and Logical operations labels Apr 20, 2017
@jreback jreback added this to the Next Major Release milestone Apr 20, 2017
@RhysU
Copy link
Author

RhysU commented Apr 24, 2017

Thank you @wuisawesome and @jreback.

deniederhut added a commit to deniederhut/pandas that referenced this issue Jan 18, 2018
Adds special method for unary plus to ndframe generics, and restricts the
allowed dtypes for the unary negative. Closes pandas-dev#16073.
@jreback jreback modified the milestones: Next Major Release, 0.23.0 Jan 18, 2018
deniederhut added a commit to deniederhut/pandas that referenced this issue Jan 23, 2018
Adds special method for unary plus to ndframe generics, and restricts the
allowed dtypes for the unary negative. Closes pandas-dev#16073.
deniederhut added a commit to deniederhut/pandas that referenced this issue Jan 29, 2018
Adds special method for unary plus to ndframe generics, and restricts the
allowed dtypes for the unary negative. Closes pandas-dev#16073.
deniederhut added a commit to deniederhut/pandas that referenced this issue Feb 2, 2018
Adds special method for unary plus to ndframe generics, and restricts the
allowed dtypes for the unary negative. Closes pandas-dev#16073.
deniederhut added a commit to deniederhut/pandas that referenced this issue Feb 5, 2018
Adds special method for unary plus to ndframe generics, and restricts the
allowed dtypes for the unary negative. Closes pandas-dev#16073.
deniederhut added a commit to deniederhut/pandas that referenced this issue Feb 7, 2018
Adds special method for unary plus to ndframe generics, and restricts the
allowed dtypes for the unary negative. Closes pandas-dev#16073.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Compat pandas objects compatability with Numpy or Python functions Enhancement good first issue Numeric Operations Arithmetic, Comparison, and Logical operations
Projects
None yet
3 participants