Skip to content

Unary plus missing for Series and DataFrame #16073

Closed
@RhysU

Description

@RhysU

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Compatpandas objects compatability with Numpy or Python functionsEnhancementNumeric OperationsArithmetic, Comparison, and Logical operationsgood first issue

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions