Skip to content

BUG: np.round does not work on a series in 0.18.0 #12600

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
wholmgren opened this issue Mar 12, 2016 · 16 comments
Closed

BUG: np.round does not work on a series in 0.18.0 #12600

wholmgren opened this issue Mar 12, 2016 · 16 comments
Labels
Compat pandas objects compatability with Numpy or Python functions
Milestone

Comments

@wholmgren
Copy link
Contributor

First, conda install pandas is currently pulling down 0.18.0, which was unexpected since I only see RCs right now, but, on to the real issue...

In 0.18.0 I cannot pass a series to np.round...

In [35]: np.round(pd.Series([1.11, 2.21]), 1)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-35-4cc66a785b99> in <module>()
----> 1 np.round(pd.Series([1.11, 2.21]), 1)

/Users/holmgren/miniconda3/envs/pvlib35/lib/python3.5/site-packages/numpy/core/fromnumeric.py in round_(a, decimals, out)
   2784     except AttributeError:
   2785         return _wrapit(a, 'round', decimals, out)
-> 2786     return round(decimals, out)
   2787
   2788

TypeError: round() takes from 1 to 2 positional arguments but 3 were given

np.round does still work on a DataFrame in 0.18.0...

In [5]: np.round(pd.DataFrame([1.11,2.22]), 1)
Out[5]:
     0
0  1.1
1  2.2

A Series on pandas 0.17.1...

In [4]: np.round(pd.Series([1.12, 2.22]), 1)
Out[4]:
0    1.1
1    2.2
dtype: float64

Ok, I know that there's now a round method in pandas (which is great), but I like to use np.round in unit tests so that I can test against earlier versions of pandas.

Version info:

In [34]: pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 3.5.1.final.0
python-bits: 64
OS: Darwin
OS-release: 15.3.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.18.0
nose: 1.3.7
pip: 8.1.0
setuptools: 20.2.2
Cython: None
numpy: 1.10.4
scipy: 0.17.0
statsmodels: None
xarray: None
IPython: 4.1.1
sphinx: 1.3.5
patsy: None
dateutil: 2.4.2
pytz: 2015.7
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 1.5.1
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.8
boto: None
@jreback
Copy link
Contributor

jreback commented Mar 12, 2016

this is the same issue as in #12238 and solved for np.searchsorted in #12413 by @gfyoung

The issue is numpy insists on call with positional arguments a 'sub-class' method, even though we are duck like, to implement things like this, which is fine. but its just not very friendlty.

so we need to accept (but not advertise the out) arg (which was taken out in 0.18.0).

separately, @gfyoung I think numpy should have a try: except: around _wrap_it to guard against things like this.

@jreback jreback added this to the 0.18.1 milestone Mar 12, 2016
@jreback jreback added Compat pandas objects compatability with Numpy or Python functions Difficulty Intermediate labels Mar 12, 2016
@jreback
Copy link
Contributor

jreback commented Mar 12, 2016

0.18.0 has been released to conda, and I just put the docs up, but hasn't gone to PyPi yet, nor release email; but thanks for the report.

@gfyoung
Copy link
Member

gfyoung commented Mar 12, 2016

  1. I'll grab this issue and fix it. Shouldn't be too difficult to fix on the pandas end.
  2. For the try-except block, what would you suggest for the except part?

@jreback
Copy link
Contributor

jreback commented Mar 12, 2016

@gfyoung same thing as if .round doesn't exist. The problem is numpy is trying to dispatch to something which is says, hey you have a .round method AND you must use this signature (which numpy uses), which is pretty incompatible with anything remotely duck-like.

@gfyoung
Copy link
Member

gfyoung commented Mar 12, 2016

Oh, oh, okay, I see what you mean. I have a PR opened against numpy that updates the function signatures, but I'll update the PR to make sure the behaviour is consistent across all of those fromnumeric.py functions to implement that try-except functionality you are describing (I think some of them do but not all of them AFAIK)

@gfyoung
Copy link
Member

gfyoung commented Mar 12, 2016

Also, for future reference, what's the oldest numpy version that this 0.18.0 release will support?

@wholmgren
Copy link
Contributor Author

thanks @jreback and @gfyoung!

@jreback
Copy link
Contributor

jreback commented Mar 12, 2016

@gfyoung numpy 1.7

@Dr-Irv
Copy link
Contributor

Dr-Irv commented Mar 14, 2016

I removed the "out" argument when implementing support for built-in round based on discussion here:, where @jreback said: "so drop the out paramater from Series.round (you can put that in API changes). We don't support this ANYWHERE else so its really inconsitent."

@gfyoung
Copy link
Member

gfyoung commented Mar 14, 2016

@Dr-Irv : Yeah, unfortunately numpy didn't get the memo. 😄 However, it's a problem that's a part of a much bigger issue with compatibility between these two libraries.

@jreback
Copy link
Contributor

jreback commented Mar 14, 2016

@Dr-Irv no worries, it is the right thing to do. as @gfyoung its a bigger issue which has recently surfaced in laster versions of numpy.

@Twizzledrizzle
Copy link

Are we having problem with this bug as well? On my computer with the latest Anaconda, win7 it works fine. But on a collegue's, we started getting error numpy round() takes at most 2 arguments (3 given) when he installed the latest version.

this for example works for me, but not for him

import pandas as pd
import numpy as np
a = [1.23, 1.33]
print np.round(pd.Series(a), 1)

As a work-around, making below change solved it, but I do not know if I have broken something else. Our scripts seems to work though

2016-03-17_09-26-54

@gfyoung
Copy link
Member

gfyoung commented Mar 17, 2016

@Twizzledrizzle : Unfortunately, the fix isn't that simple. Your change is actually modifying fromnumeric.py, which is in the numpy library and not in the pandas library. Perhaps this works for you if you're never sending the result to another array out, but universally, this will not work I'm afraid because it modifies a library that we have no control over for all intensive purposes.

@Twizzledrizzle
Copy link

In what version do you think series will be compatible with np.round again? Thank you for all your time on this! Still using our ugly fix in fromnumeric.py

Or is it recommended to use series.round(...) instead? Unfortunately many places to fix in that case in our codebase.

@gfyoung
Copy link
Member

gfyoung commented May 30, 2016

Should be compatible in 0.18.1 but I would recommend using series round anyways (sorry! 😞), as maintaining total compatibly with numpy's implementation is not something we want to do for this library in light of discussions we have had previously.

@Twizzledrizzle
Copy link

Thank you gfyoung! I will try some find & replace magic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Compat pandas objects compatability with Numpy or Python functions
Projects
None yet
Development

No branches or pull requests

5 participants