Skip to content

Inconsistency in array_to_datetime "utc_convert" #19623

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
jbrockmendel opened this issue Feb 9, 2018 · 6 comments
Closed

Inconsistency in array_to_datetime "utc_convert" #19623

jbrockmendel opened this issue Feb 9, 2018 · 6 comments
Labels
Timezones Timezone data dtype

Comments

@jbrockmendel
Copy link
Member

If tslib.array_to_datetime sees a tz-aware datetime object and utc_convert isn't True, it raises. But later a tz-aware datetime may be parsed from dateutil and it is ignored:

            elif PyDateTime_Check(val):
                seen_datetime = 1
                if val.tzinfo is not None:
                    if utc_convert:
                        _ts = convert_datetime_to_tsobject(val, None)
                        iresult[i] = _ts.value
                        try:
                            check_dts_bounds(&_ts.dts)
                        except ValueError:
                            if is_coerce:
                                iresult[i] = NPY_NAT
                                continue
                            raise
                    else:
                        raise ValueError('Tz-aware datetime.datetime cannot '
                                         'be converted to datetime64 unless '
                                         'utc=True')
[...]
                    try:
                        py_dt = parse_datetime_string(val, dayfirst=dayfirst,
                                                      yearfirst=yearfirst)
                    except Exception:
                        if _parse_today_now(val, &iresult[i]):
                            continue
                        if is_coerce:
                            iresult[i] = NPY_NAT
                            continue
                        raise TypeError("invalid string coercion to datetime")

                    try:
                        _ts = convert_datetime_to_tsobject(py_dt, None)
                        iresult[i] = _ts.value
                    except ValueError:
                        if is_coerce:
                            iresult[i] = NPY_NAT
                            continue
                        raise

There are several ways (some of which are hit in the tests) by which parse_datetime_string could return a tzaware datetime. Should these raise iff utc_convert is not True? Or behave like the string_to_dts parsing above and allow FixedOffset through? What about if it is aware and already in UTC?

@jreback
Copy link
Contributor

jreback commented Feb 10, 2018

is there a user facing effect to this?

@jbrockmendel
Copy link
Member Author

tstr = '2018-02-10 10:16:06 PST'
ts = pd.Timestamp(tstr)   # comes back with tzlocal()
>>> pd.to_datetime([ts])
DatetimeIndex(['2018-02-10 10:16:06-08:00'], dtype='datetime64[ns, tzlocal()]', freq=None)
>>> pd.to_datetime([tstr])
DatetimeIndex(['2018-02-10 18:16:06'], dtype='datetime64[ns]', freq=None)

@jreback
Copy link
Contributor

jreback commented Feb 10, 2018

ok, pls check outstanding issues, I think have seen this before.

@jreback jreback added the Timezones Timezone data dtype label Feb 10, 2018
@jbrockmendel
Copy link
Member Author

@mroeschke did the recent arraytodatetime change handle this?

@mroeschke
Copy link
Member

In [7]: pd.to_datetime([ts])
Out[7]: DatetimeIndex(['2018-02-10 10:16:06-08:00'], dtype='datetime64[ns, tzlocal()]', freq=None)

In [8]: pd.to_datetime([tstr])
Out[8]: DatetimeIndex(['2018-02-10 10:16:06-08:00'], dtype='datetime64[ns, pytz.FixedOffset(-480)]', freq=None)

So a little better. Is the ultimate goal is for PST to parse to 'US/Pacific'? (xref #18702 (comment))?

@jbrockmendel
Copy link
Member Author

Is the ultimate goal is for PST to parse to 'US/Pacific'?

Yes, but I think #18702 is orthogonal. Closing.

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

No branches or pull requests

3 participants