Skip to content
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

Series is missing real/imag attributes #4819

Closed
twmr opened this issue Sep 11, 2013 · 11 comments · Fixed by #4820
Closed

Series is missing real/imag attributes #4819

twmr opened this issue Sep 11, 2013 · 11 comments · Fixed by #4820
Labels
API Design Numeric Operations Arithmetic, Comparison, and Logical operations Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@twmr
Copy link

twmr commented Sep 11, 2013

The Series class in 0.10 had a real and a imag attribute but in 0.12dev these attrs are missing. This probably happend during the refactoring of the Series class for 0.12.

In [1]: pd.__version__
Out[1]: '0.10.0'

In [2]: a = np.arange(5)

In [3]: b = pd.Series(a + 4j*a)

In [4]: b.real
Out[4]: 
0    0
1    1
2    2
3    3
4    4
In [1]: pd.__version__
Out[1]: '0.12.0-381-g15d5f0a'

In [2]: a = np.arange(5)

In [3]: b = pd.Series(a + 4j*a)

In [4]: b.real
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-ec5ddb077f9e> in <module>()
----> 1 b.real

/usr/lib64/python2.7/site-packages/pandas-0.12.0_381_g15d5f0a-py2.7-linux-x86_64.egg/pandas/core/generic.pyc in __getattr__(self, name)
   1287             return self[name]
   1288         raise AttributeError("'%s' object has no attribute '%s'" %
-> 1289                              (type(self).__name__, name))
   1290 
   1291     def __setattr__(self, name, value):

AttributeError: 'Series' object has no attribute 'real'
@jreback
Copy link
Contributor

jreback commented Sep 11, 2013

you can access like below

These are easy to do (just a delegated property), but is this a typical access pattern?

should we make them methods? what do you do with the complex numbers?

In [1]: a = np.arange(5)

In [2]: 

In [2]: In [3]: b = pd.Series(a + 4j*a)

In [3]: b
Out[3]: 
0         0j
1     (1+4j)
2     (2+8j)
3    (3+12j)
4    (4+16j)
dtype: complex128

In [4]: b.values
Out[4]: array([ 0. +0.j,  1. +4.j,  2. +8.j,  3.+12.j,  4.+16.j])

In [5]: b.values.real
Out[5]: array([ 0.,  1.,  2.,  3.,  4.])

In [6]: b.values.imag
Out[6]: array([  0.,   4.,   8.,  12.,  16.])

@twmr
Copy link
Author

twmr commented Sep 11, 2013

I know that - actually, this is what I'm using as a workaround in 0.12dev. Is it intentional that the 'new' Series class does not contain these attrs anymore?

@jreback
Copy link
Contributor

jreback commented Sep 11, 2013

nope...they are apparently properties on numpy ararys; we didn't have any tests that use these so they weren't 'missing'. I'll add them..pretty easy

@jreback
Copy link
Contributor

jreback commented Sep 11, 2013

@Thisch ok...all fixed up...updated to current master and give a try..thanks for the report

@twmr
Copy link
Author

twmr commented Sep 11, 2013

Thank you.

Sry that I have not answered all your questions in your first reply. Atm I use complex numbers mainly for working with 2D positions (rotating these positions around a certain point is much easier if the are stored as complex numbers - exp(1j*phi) vs 2x2 Rotationmatrix)).

Simple delegation of the properties is fine

@twmr
Copy link
Author

twmr commented Sep 11, 2013

Support for abs() should be added as well. This makes sense for real valued data as well.

In 0.10:

In [4]: b.abs()
Out[4]: 
0     0.000000
1     4.123106
2     8.246211
3    12.369317
4    16.492423

In [5]: abs(b)
Out[5]: 
0     0.000000
1     4.123106
2     8.246211
3    12.369317
4    16.492423

I tried to fix this by adding

    @rwproperty.getproperty
    def __abs__(self):
        return abs(self.values)

to the series class. This has the effect that b.abs() now works but abs(b) doesn't (TypeError: 'numpy.ndarray' object is not callable). Any ideas ?

@jtratner
Copy link
Contributor

why would that be a property?

@jreback
Copy link
Contributor

jreback commented Sep 11, 2013

This is already defined

In [1]:  a = np.arange(5)

In [2]:         b = Series(a + 4j*a)

In [3]: b
Out[3]: 
0         0j
1     (1+4j)
2     (2+8j)
3    (3+12j)
4    (4+16j)
dtype: complex128

In [4]: b.abs()
Out[4]: 
0     0.000000
1     4.123106
2     8.246211
3    12.369317
4    16.492423
dtype: float64

In [5]: np.abs(b)
Out[5]: 
0     0.000000
1     4.123106
2     8.246211
3    12.369317
4    16.492423
dtype: float64

@twmr
Copy link
Author

twmr commented Sep 11, 2013

You are right, the property decorator is wrong and both b.abs() an np.abs(b) already work. Some of my code uses abs(b) instead of np.abs(b) and this is raising an exception:

In [5]: abs(b)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-de066fb5b1d7> in <module>()
----> 1 abs(b)

TypeError: bad operand type for abs(): 'Series'

I can change my code to use np.abs instead - I just want you to know that abs(Series) is no longer working in 0.12dev.

@jtratner
Copy link
Contributor

I have a PR brewing...it'll be in master shortly.

On Wed, Sep 11, 2013 at 7:09 PM, thomas notifications@github.com wrote:

You are right, the property decorator is wrong and both b.abs() an
np.abs(b) already work. Some of my code uses abs(b) instead of np.abs(b)and this is raising an exception:

In [5]: abs(b)

TypeError Traceback (most recent call last)
in ()
----> 1 abs(b)

TypeError: bad operand type for abs(): 'Series'

I can change my code to use np.abs instead - I just want you to know that
abs(Series) is no longer working in 0.12dev.


Reply to this email directly or view it on GitHubhttps://github.com//issues/4819#issuecomment-24283691
.

@twmr
Copy link
Author

twmr commented Sep 15, 2013

Great. Thx for your quick fixes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Numeric Operations Arithmetic, Comparison, and Logical operations Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants