Skip to content

Accepts integer/float string with units and raises when unit is ambiguous (2) #23025

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
wants to merge 50 commits into from

Conversation

Sup3rGeo
Copy link
Contributor

@Sup3rGeo Sup3rGeo commented Oct 7, 2018

Supersedes #21384.

@pep8speaks
Copy link

pep8speaks commented Oct 7, 2018

Hello @Sup3rGeo! Thanks for updating the PR.

Cheers ! There are no PEP8 issues in this Pull Request. 🍻

Comment last updated on January 08, 2019 at 15:23 Hours UTC

@Sup3rGeo
Copy link
Contributor Author

Sup3rGeo commented Oct 7, 2018

@jorisvandenbossche Ok so I created another PR, we can continue on this one

@Sup3rGeo
Copy link
Contributor Author

Sup3rGeo commented Oct 7, 2018

One point we have to decide: So far, Timedelta('3.1416') raised and Timedelta('2000') passed.

What do we want to do?

In this PR I am just raising for Timedelta('2000') as well and updated all tests that were not following this.

@Sup3rGeo Sup3rGeo changed the title Accepts integer/float string with units and raises when unit is ambiguous (2) [WIP] Accepts integer/float string with units and raises when unit is ambiguous (2) Oct 7, 2018
@jreback jreback added Timedelta Timedelta data type Error Reporting Incorrect or improved errors from pandas labels Oct 7, 2018
@Sup3rGeo
Copy link
Contributor Author

The failure in TravisCI seems to be something unrelated to this feature in question. Is there anything I should change related to that?

@Sup3rGeo
Copy link
Contributor Author

@jreback I really don't know what to do to make this failing test in Travis CI pass. I need some help/pointers here.

@@ -287,7 +287,7 @@ cdef inline _decode_if_necessary(object ts):
return ts


cdef inline parse_timedelta_string(object ts):
cpdef inline parse_timedelta_string(object ts, specified_unit=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

if unit is None:
return 'ns'
elif unit == 'M':
if unit is None or unit == 'M':
return unit
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes this is ok, can you add a comment

@@ -1132,9 +1132,9 @@ Timedelta
- Fixed bug in adding a :class:`DataFrame` with all-`timedelta64[ns]` dtypes to a :class:`DataFrame` with all-integer dtypes returning incorrect results instead of raising ``TypeError`` (:issue:`22696`)
- Bug in :class:`TimedeltaIndex` where adding a timezone-aware datetime scalar incorrectly returned a timezone-naive :class:`DatetimeIndex` (:issue:`23215`)
- Bug in :class:`TimedeltaIndex` where adding ``np.timedelta64('NaT')`` incorrectly returned an all-`NaT` :class:`DatetimeIndex` instead of an all-`NaT` :class:`TimedeltaIndex` (:issue:`23215`)
- Bug in :class:`Timedelta` where passing a string of a pure number would not take unit into account. Also raises for ambiguous/duplicate unit specification (:issue:`12136`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also mention to_timedelta, and also the change that unit now needs to be specified?

else:
if have_value:
raise ValueError("have leftover units")
if len(number):
r = timedelta_from_spec(number, frac, 'ns')
result += timedelta_as_neg(r, neg)
raise ValueError("number string without units")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I mentioned it before, but I think we need to deprecate this first. We can here raise a deprecation warning when unit is None, and still fallback to unit='ns' for now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok!

@@ -1224,9 +1224,9 @@ Timedelta
- Fixed bug in adding a :class:`DataFrame` with all-`timedelta64[ns]` dtypes to a :class:`DataFrame` with all-integer dtypes returning incorrect results instead of raising ``TypeError`` (:issue:`22696`)
- Bug in :class:`TimedeltaIndex` where adding a timezone-aware datetime scalar incorrectly returned a timezone-naive :class:`DatetimeIndex` (:issue:`23215`)
- Bug in :class:`TimedeltaIndex` where adding ``np.timedelta64('NaT')`` incorrectly returned an all-`NaT` :class:`DatetimeIndex` instead of an all-`NaT` :class:`TimedeltaIndex` (:issue:`23215`)
- Bug in :class:`Timedelta` (and :func: `to_timedelta`) where passing a string of a pure number would not take unit into account. Also raises for ambiguous/duplicate unit specification and a number string without defined unit is deprecated (:issue:`12136`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug in :class:`Timedelta` (and :func: `to_timedelta`) where passing a string of a pure number would not take the unit into account. Now raises for an ambiguous or duplicate unit specification.(:issue:`12136`)

put in the deprecation section
A timedelta passed a number string without a defined unit is deprecated (:issue:`12136`)

@@ -165,6 +165,8 @@ cpdef convert_to_timedelta64(object ts, object unit):
# kludgy here until we have a timedelta scalar
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can make unit=None in the signature, can you add a doc-string as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, what should I add to the docstring exactly?

warnings.warn(
"number string without units is deprecated and
" will raise an exception in future versions. Considering as nanoseconds.",
DeprecationWarning
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use FutureWarning

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok!

return 'ns'
elif unit == 'M':

# Preserve, will be dealt with eventually in other functions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does this comment mean? can you reword to reflect your answer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, will try with a better comment.

@@ -521,6 +521,7 @@ def sequence_to_td64ns(data, copy=False, unit="ns", errors="raise"):
# treat as multiples of the given unit. If after converting to nanos,
# there are fractional components left, these are truncated
# (i.e. NOT rounded)
unit = unit if unit is not None else "ns"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if unit is None:
    unit = 'ns'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok!

@jreback
Copy link
Contributor

jreback commented Dec 3, 2018

can you merge master and i will look

…edelta2

# Conflicts:
#	doc/source/whatsnew/v0.24.0.rst
#	pandas/tests/util/test_hashing.py
@jreback
Copy link
Contributor

jreback commented Dec 23, 2018

can you merge master and update

@jreback
Copy link
Contributor

jreback commented Jan 5, 2019

can you merge master

Victor Maryama added 3 commits January 7, 2019 16:36
…edelta2

# Conflicts:
#	doc/source/whatsnew/v0.24.0.rst
#	pandas/tests/frame/test_arithmetic.py
@Sup3rGeo
Copy link
Contributor Author

Sup3rGeo commented Jan 8, 2019

Waiting for a seemingly bug in pytest

pytest-dev/pytest#4617

@WillAyd
Copy link
Member

WillAyd commented Feb 27, 2019

@Sup3rGeo can you merge master here? Looks like the relevant issue you linked is closed

@jreback
Copy link
Contributor

jreback commented Mar 20, 2019

can you merge master

@jreback
Copy link
Contributor

jreback commented Apr 5, 2019

would love to see this, can you merge master and update

@WillAyd
Copy link
Member

WillAyd commented May 3, 2019

Would be nice but I think this has gone stale. @Sup3rGeo please ping if you'd like to pick this one back up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Error Reporting Incorrect or improved errors from pandas Timedelta Timedelta data type
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG/API: to_timedelta unit-argument ignored for string input
6 participants