Skip to content

Arithmetic by DataFrame index #7439

Closed
Closed
@mmajewsk

Description

@mmajewsk

I encountered a problem with doing any arythmetic from index, in other words, when a index is time (datetime64) and i would like to count something by it, i have no other option than to assign it to some column in dataframe object.
import pandas as pd

import pandas as pd

rng = pd.date_range('1/1/2011', periods=4, freq='H')
ts = pd.Series(rng, index=rng)

print "Data:"
print ts

print "\nSubstraction from column"
print ts-ts[0]

print "\nIndex to column"
ts['lol']=ts.index
print ts['lol']-ts['lol'][0]

print "\nSubstraction by index"
df = ts.index
print df-df[0]

result:

Data:
2011-01-01 00:00:00   2011-01-01 00:00:00
2011-01-01 01:00:00   2011-01-01 01:00:00
2011-01-01 02:00:00   2011-01-01 02:00:00
2011-01-01 03:00:00   2011-01-01 03:00:00
Freq: H, dtype: datetime64[ns]

Substraction from column
2011-01-01 00:00:00   00:00:00
2011-01-01 01:00:00   01:00:00
2011-01-01 02:00:00   02:00:00
2011-01-01 03:00:00   03:00:00
Freq: H, dtype: timedelta64[ns]

Index to column
lol   00:00:00
lol   01:00:00
lol   02:00:00
lol   03:00:00
dtype: timedelta64[ns]

Substraction by index
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-146-5a8539747b5a> in <module>()
     13 print "\nSubstraction by index"
     14 df = ts.index
---> 15 print df-df[0]
     16 

C:\winpy\WinPython-64bit-2.7.6.4\python-2.7.6.amd64\lib\site-packages\pandas\core\index.pyc in __sub__(self, other)
    853 
    854     def __sub__(self, other):
--> 855         return self.diff(other)
    856 
    857     def __and__(self, other):

C:\winpy\WinPython-64bit-2.7.6.4\python-2.7.6.amd64\lib\site-packages\pandas\core\index.pyc in diff(self, other)
    981 
    982         if not hasattr(other, '__iter__'):
--> 983             raise TypeError('Input must be iterable!')
    984 
    985         if self.equals(other):

TypeError: Input must be iterable!

Maybe it's just conceptional problem, but if i want to make something with date index i have to keep additional column (with the same values as index!).
When it comes to huge datasets this can be a problem, because i have to store the same thing twice, or make additional column for calculations, which is not better.

P.S. pd.show_versions():

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.6.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 37 Stepping 2, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None

pandas: 0.13.1
Cython: 0.20.1
numpy: 1.8.1
scipy: 0.13.3
statsmodels: 0.5.0
IPython: 2.0.0
sphinx: 1.2.2
patsy: 0.2.1
scikits.timeseries: None
dateutil: 2.2
pytz: 2013.9
bottleneck: None
tables: 3.1.1
numexpr: 2.3.1
matplotlib: 1.3.1
openpyxl: None
xlrd: 0.9.3
xlwt: 0.7.5
xlsxwriter: None
sqlalchemy: 0.9.4
lxml: None
bs4: None
html5lib: None
bq: None
apiclient: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    API DesignDocsIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions