-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Support for PEP 3141 numbers #22952
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
Support for PEP 3141 numbers #22952
Conversation
PROBLEMcalling FIXcalling |
Thanks for the fix @brute4s99 Can you add a test (a test that fails before the fix, and that passes with it), and a line in the bugs section of the whatsnew file for 0.24 please? |
sure, @datapythonista ! I didn't know about the whatsnew file, so thanks ! |
no problem, took me several PRs before I started remembering about the whatsnew myself ;) |
pandas/_libs/lib.pyx
Outdated
or util.is_offset_object(val)) | ||
|
||
or util.is_offset_object(val) | ||
or np.isscalar(val)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does numpy's C-API have an implementation of this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the behavior vary across supported versions of numpy?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does numpy's C-API have an implementation of this?
I went this way, @jbrockmendel
Further, ScalarType
is in numerictypes.py here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would just import Number / Fraction here and do an isinstance check
Simple solution, may well be the best one. This will definitely need a performance check. |
where can I connect with all the devs, @datapythonista ? |
Codecov Report
@@ Coverage Diff @@
## master #22952 +/- ##
=======================================
Coverage 92.28% 92.28%
=======================================
Files 161 161
Lines 51451 51451
=======================================
Hits 47483 47483
Misses 3968 3968
Continue to review full report at Codecov.
|
I just thought of reusing exisitng code, since we already require |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pls add a test
pandas/_libs/lib.pyx
Outdated
or util.is_offset_object(val)) | ||
|
||
or util.is_offset_object(val) | ||
or np.isscalar(val)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would just import Number / Fraction here and do an isinstance check
doc/source/whatsnew/v0.24.0.txt
Outdated
False | ||
|
||
New Behavior: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you don't need this whole sub-section ,this is a minor function, however adding these examples to the doc-string would be ok.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you don't need this whole sub-section ,this is a minor function, however adding these examples to the doc-string would be ok.
Which doc-string are you referring to, @jreback ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the is_scalar doc-strings, though I dont' think we have an Examples section, if you can add one with some examples would be great, have a look at infer_dtype
for some inspiration on a good doc-string
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do, @jreback .
@brute4s99 you can use gitter: https://gitter.im/pydata/pandas If it's related to this PR, is usually better to discuss here, so other people interested/reviewing are aware of the discussions. In this case the issue is that the file you're modifying is a |
There's gitter, though I don't know how much it is used. The conversation here in GH likely your best bet.
That's a good intuition, and may end up being the optimal way to go, but in the cython code we make an extra effort to avoid making pure-python calls. So if there turned out to be a C-implementation, we'd want to use that. As it is I expect @jreback's suggestion to directly do an isinstance check is the easiest solution. |
I was thinking the same, but then dropped it presuming importing Fraction and Number might cause slowness. |
Can you please elaborate, @jreback ? |
You need to add a test that shows that your changes all work as expected. |
Somewhere around here: pandas/pandas/tests/dtypes/test_inference.py Line 1119 in 1a12c41
|
Hello @brute4s99! Thanks for updating the PR.
Comment last updated on October 03, 2018 at 09:54 Hours UTC |
Tests have been added and passed ! 😄 |
please ignore failed Travis CI : :issue:
|
doc/source/whatsnew/v0.24.0.txt
Outdated
@@ -834,3 +835,5 @@ Other | |||
- :meth:`DataFrame.nlargest` and :meth:`DataFrame.nsmallest` now returns the correct n values when keep != 'all' also when tied on the first columns (:issue:`22752`) | |||
- :meth:`~pandas.io.formats.style.Styler.bar` now also supports tablewise application (in addition to rowwise and columnwise) with ``axis=None`` and setting clipping range with ``vmin`` and ``vmax`` (:issue:`21548` and :issue:`21526`). ``NaN`` values are also handled properly. | |||
- Logical operations ``&, |, ^`` between :class:`Series` and :class:`Index` will no longer raise ``ValueError`` (:issue:`22092`) | |||
- Checking PEP 3141 numbers in `pandas.api.types.is_scalar` function returns ``True`` (:issue:`22903`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use the :func:`~pandas.api.types.is_scalar`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, @jreback
pandas/_libs/lib.pyx
Outdated
|
||
Parameters | ||
---------- | ||
val : numpy array scalar (e.g. np.int64), Python builtin numerics, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@datapythonista @jorisvandenbossche is this how we format lists of things?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brute4s99 this should be like:
val : single line type description
The full parameter description with indent of 4 spaces (this can
span over multiple lines)
If the type description is a long enumeration (like in this case) and doesn't fit on a single line, you can keep the actual type description vague (eg "scalar"), and then put all the possibilities in the parameter description.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for clarification, @jorisvandenbossche ! Will follow suit .
pandas/_libs/lib.pyx
Outdated
|
||
Examples | ||
-------- | ||
>>> dt = pd.datetime.datetime(2018,10,3) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you show an example with a list as well (to return False)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure! will do !
pandas/_libs/lib.pyx
Outdated
or util.is_offset_object(val)) | ||
|
||
or util.is_offset_object(val) | ||
or isinstance(val, Number) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use a compound isinstance(val, (Number, Fraction))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh! I didn't know that !
Thanks @jreback
@@ -13,7 +13,8 @@ | |||
import numpy as np | |||
import pytz | |||
import pytest | |||
|
|||
from numbers import Number |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you put the builtin imports at the top
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, sure!
@@ -1184,6 +1185,10 @@ def test_is_scalar_pandas_containers(self): | |||
assert not is_scalar(Index([])) | |||
assert not is_scalar(Index([1])) | |||
|
|||
def tes_is_scalar_pep_3141(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just add onto the test_is_scalar_builtin_scalars
test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried it, @jreback . Strangely, it was throwing errors, and local tests were failing. That's why I defined it in a different function. I couldn't understand the error either, let me send you a snip.
@jreback please review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks fine. can you rebase, ping on green.
-------- | ||
>>> dt = pd.datetime.datetime(2018,10,3) | ||
>>> pd.is_scalar(dt) | ||
True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you put a blank line between cases (as you have done for some)
@jreback please review, I just re-did the changes after a |
e7baf2b
to
2d10e8b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some issues in the documentation, for the rest looks good
pandas/_libs/lib.pyx
Outdated
|
||
Parameters | ||
---------- | ||
val : input argument of any type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After the name of the parameter and the colon, just but the name of the expected Python type. In this case use object
if anything can be used.
pandas/_libs/lib.pyx
Outdated
|
||
Parameters | ||
---------- | ||
val : input argument of any type | ||
This includes: | ||
- numpy array scalar (e.g. np.int64) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you generate the html version of this docstring? ./doc/make.py html --single=path.to.this.function
I think this won't render properly
pandas/_libs/lib.pyx
Outdated
|
||
Returns | ||
------- | ||
True if the given value is scalar, False otherwise. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the returns first list the type (i.e. bool
) in the next line indented the description.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comments on the docs
pandas/_libs/lib.pyx
Outdated
|
||
Parameters | ||
---------- | ||
val : object | ||
This includes: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be indented, please build the html and check
pandas/_libs/lib.pyx
Outdated
* instances of datetime.datetime | ||
* instances of datetime.timedelta | ||
* Period | ||
* instances of decimal.Decimal |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you remove all the instances of
here. All the items in the list what you can check are the instances
pandas/_libs/lib.pyx
Outdated
|
||
Returns | ||
------- | ||
a bool object. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just bool
, and in the next line indented a description
pandas/_libs/lib.pyx
Outdated
>>> pd.api.types.is_scalar([2, 3]) | ||
False | ||
|
||
>>> pd.api.types.is_scalar({0:1, 2:3}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there are missing spaces after the colon, check pep8 for those
pandas/_libs/lib.pyx
Outdated
True | ||
|
||
>>> from numbers import Number | ||
>>> pd.api.types.is_scalar(Number()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it makes more sense to have an example Number
here. Not sure what an empty Number
instance can be used for, but doesn't seem something you'd use as often as an actual number.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What should I put in here, then, @datapythonista ?
Any number would fall into int
category... :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah prob can remove this last Number example, its not generally useful unless you actually inherit from Number of a subclass.
can you merge master and update |
can you merge master |
please review, @datapythonista @jreback |
rebase. small change @brute4s99 |
is this done right? @jreback |
Check the diff of the PR, something went wrong and it has lots of unrelated changes. Can you fix it please, so we can move forward with it. |
okayyy, I jumbled up. |
please review, @datapythonista |
I accidentally rebased on origin/master, that was ~350 commits behind upstream/master
|
@datapythonista over to you; merge when satisfied. |
Thanks @brute4s99 |
…fixed * upstream/master: DOC: Removing rpy2 dependencies, and converting examples using it to regular code blocks (pandas-dev#23737) BUG: Fix dtype=str converts NaN to 'n' (pandas-dev#22564) DOC: update pandas.core.resample.Resampler.nearest docstring (pandas-dev#20381) REF/TST: Add more pytest idiom to parsers tests (pandas-dev#23810) Added support for Fraction and Number (PEP 3141) to pandas.api.types.is_scalar (pandas-dev#22952) DOC: Updating to_timedelta docstring (pandas-dev#23259)
git diff upstream/master -u -- "*.py" | flake8 --diff