Skip to content

API: arithmetic ops could accept an index as appropriate #4629

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
jreback opened this issue Aug 21, 2013 · 6 comments · Fixed by #4641
Closed

API: arithmetic ops could accept an index as appropriate #4629

jreback opened this issue Aug 21, 2013 · 6 comments · Fixed by #4641
Labels
API Design Dtype Conversions Unexpected or buggy dtype conversions Numeric Operations Arithmetic, Comparison, and Logical operations
Milestone

Comments

@jreback
Copy link
Contributor

jreback commented Aug 21, 2013

Not sure if this will cause weirdness but haveing the arithmetic operations able to accept an index (when appropriate type), e.g. a datecolumn should be able to interact with a DatetimeIndex, but prob should prohibit an int column from interacting with a IntIndex?

e.g. in below df['date']-df.index should work

This actually DOES work now! (and I take it back, does make some sense)
should df[1]-df.reset_index().index work????

http://stackoverflow.com/questions/18368442/how-to-directly-use-pandas-date-time-index-in-calculations/18368793?noredirect=1#comment26971689_18368793

In [1]: df = DataFrame(randn(5,2),index=date_range('20130101',periods=5))

In [3]: df['date'] = Timestamp('20130102')

In [4]: df
Out[4]: 
                   0         1                date
2013-01-01  2.406932 -0.313473 2013-01-02 00:00:00
2013-01-02  0.034162 -0.708450 2013-01-02 00:00:00
2013-01-03 -1.585031  0.587243 2013-01-02 00:00:00
2013-01-04  1.454818  1.089208 2013-01-02 00:00:00
2013-01-05 -0.778016 -0.994242 2013-01-02 00:00:00

In [5]: df['td'] = df['date']-df.index.to_series()

In [6]: df
Out[6]: 
                   0         1                date                td
2013-01-01  2.406932 -0.313473 2013-01-02 00:00:00  1 days, 00:00:00
2013-01-02  0.034162 -0.708450 2013-01-02 00:00:00          00:00:00
2013-01-03 -1.585031  0.587243 2013-01-02 00:00:00 -1 days, 00:00:00
2013-01-04  1.454818  1.089208 2013-01-02 00:00:00 -2 days, 00:00:00
2013-01-05 -0.778016 -0.994242 2013-01-02 00:00:00 -3 days, 00:00:00
@jtratner
Copy link
Contributor

@jreback why would you expect this not to work? Index is just an ndarray with some ops disabled. It won't work with a MultiIndex and I don't think there's a clear reason to make that work.

@jreback
Copy link
Contributor Author

jreback commented Aug 21, 2013

@jtratner no i expect it TO work

I guess there is some setting going on, so need to just fix it (e.g. just do index.to_series) is prob enough
I think only matters for DatetimeIndex (as it hits that special loop)

In [16]: df['date']-df.index
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-16-bdf30f864601> in <module>()
----> 1 df['date']-x

/usr/local/lib/python2.7/site-packages/pandas-0.12.0-py2.7-linux-x86_64.egg/pandas/core/series.pyc in wrapper(self, other, name)
    154 
    155             lvalues = lvalues.view('i8')
--> 156             rvalues = rvalues.view('i8')
    157 
    158         if isinstance(rvalues, Series):

AttributeError: can't set attribute

@jtratner
Copy link
Contributor

@jreback fun, no traceback either?

@jreback
Copy link
Contributor Author

jreback commented Aug 21, 2013

yep..just bad error message....think need to add a check if the lvalues/rvalues are an Index, :( sigh

@jtratner
Copy link
Contributor

@jreback are you sure this isn't something wrong with the way Index is currently defined (e.g., something overriding view(), like an aliasing to copy that then thinks that it's the first positional argument tot the function and tries to set it)?

@jreback
Copy link
Contributor Author

jreback commented Aug 22, 2013

@jtratner #4641 fixes it...no biggie

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Dtype Conversions Unexpected or buggy dtype conversions Numeric Operations Arithmetic, Comparison, and Logical operations
Projects
None yet
2 participants