Skip to content

BUG: to_timedelta still raises with errors='ignore' #13613

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
jorisvandenbossche opened this issue Jul 11, 2016 · 2 comments
Closed

BUG: to_timedelta still raises with errors='ignore' #13613

jorisvandenbossche opened this issue Jul 11, 2016 · 2 comments
Labels
Bug Timedelta Timedelta data type
Milestone

Comments

@jorisvandenbossche
Copy link
Member

From the added docs in #13425:

In [10]: pd.to_timedelta(['apple', '1 day'], errors='ignore')
...
ValueError: unit abbreviation w/o a number

In [12]: pd.to_timedelta(['apple', pd.Timedelta('1day')], errors='ignore')
...
ValueError: unit abbreviation w/o a number

It's the same error as you get with errors='raise' (so not captured somewhere), errors='coerce is working correctly.

@jorisvandenbossche jorisvandenbossche added Bug Timedelta Timedelta data type labels Jul 11, 2016
@gfyoung
Copy link
Member

gfyoung commented Jul 26, 2016

The reason is because errors='ignore', is well, ignored. I think I would just wrap the current try-except logic as follows:

try:
    try:
        for i in range(n):
            result[i] = parse_timedelta_string(values[i])  # always raise
    except Exception:
        for i in range(n):
            result[i] = convert_to_timedelta64(values[i], unit, is_coerce)
except Exception:  # when is_coerce=False
    if errors == 'raise':
        raise
   elif errors == 'ignore':
        return values
return iresult

The reason is that one could consider the original try-except block as the process through which we convert to timedelta, then all three options for errors are satisfied:

  1. errors='raise'

Try to convert via method 1, which fails, so we try with method 2. If that fails, we raise the exception.

  1. errors='coerce' --> is_coerce=True

Try to convert via method 1, which fails, so we try with method 2. In method 2, if converting one of the elements fails, we return NPY_NAT.

  1. errors='ignore'

Try to convert via method 1, which fails, so we try with method 2. If that fails, we just return the original values array as promised in documentation.

@jorisvandenbossche jorisvandenbossche added this to the 0.19.0 milestone Jul 26, 2016
@jreback
Copy link
Contributor

jreback commented Jul 26, 2016

yeah this prob needs some logic like that

this is similar to how datetime parsing works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Timedelta Timedelta data type
Projects
None yet
Development

No branches or pull requests

3 participants