Code Sample, a copy-pastable example if possible
While pd.DataFrame().index + 1
returns the unchanged index Index([], dtype='object')
,
I see the following error with subtraction:
In [71]: pd.DataFrame().index - 1
Traceback (most recent call last):
File "<ipython-input-71-cbb1aa99c0f9>", line 1, in <module>
pd.DataFrame().index - 1
File "C:\Program Files\WinPython-64bit-3.5.2.3Qt5\python-3.5.2.amd64\lib\site-packages\pandas\indexes\base.py", line 1796, in __sub__
"{typ}".format(typ=type(self)))
TypeError: cannot perform __sub__ with this index type: <class 'pandas.indexes.base.Index'>
Problem description
I would like to have pd.DataFrame().index - 1
return the unchanged index, too.
The current behavior makes my code not generalize well for the edge case of empty dataframes (which happen in my environment).
Expected Output
The same as in the summation case.
Output of pd.show_versions()
INSTALLED VERSIONS
------------------
commit: None
python: 3.5.2.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 42 Stepping 7, GenuineIntel
byteorder: little
LC_ALL: None
LANG: en
LOCALE: None.None
pandas: 0.19.1
nose: 1.3.7
pip: 8.1.2
setuptools: 28.7.1
Cython: 0.25.1
numpy: 1.11.2
scipy: 0.18.1
statsmodels: 0.8.0rc1
xarray: 0.8.2
IPython: 5.1.0
sphinx: 1.4.8
patsy: 0.4.1
dateutil: 2.5.3
pytz: 2016.7
blosc: 1.4.4
bottleneck: 1.2.0
tables: 3.3.0
numexpr: 2.6.1
matplotlib: 1.5.3
openpyxl: None
xlrd: 1.0.0
xlwt: None
xlsxwriter: 0.9.3
lxml: 3.6.4
bs4: 4.5.1
html5lib: 0.999999999
httplib2: None
apiclient: None
sqlalchemy: 1.1.3
pymysql: None
psycopg2: None
jinja2: 2.8
boto: None
pandas_datareader: 0.2.1
Comment From: jreback
If you really want to start with a completely empty DataFrame, then do this to guarantee an integer based index. Note there are virtually no situations where I would actually start with a completely empty frame. You generally see this when indexing (and expanding) this rows by row which is non-idiomatic and non-performant.
In [3]: pd.DataFrame(index=pd.Int64Index([])).index
Out[3]: Int64Index([], dtype='int64')
In [4]: pd.DataFrame(index=pd.Int64Index([])).index - 1
Out[4]: Int64Index([], dtype='int64')