-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: _convert_and_box_cache raise ValueError: Tz-aware datetime.datetime cannot be converted to datetime64 unless utc=True #26097
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
Changes from all commits
655ec31
6a9856e
ffd9ecf
d6c584e
00f72e0
5ad9911
428cae0
7ed05f2
b60f1d5
3e2df79
3f0285e
d19c2cf
b1cf140
56db677
4f9ea36
c72a561
67a0c40
1e0d953
1942bbe
97e4548
342d7d0
12f9853
62e75f8
71ca9be
c35e124
d3412e2
3141cb6
ca200cd
1cc469e
137395f
2d8921b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
# flake8: noqa | ||
from pandas.core.indexes.api import * | ||
from pandas.core.indexes.multi import _sparsify | ||
from pandas.core.indexes.api import ( # noqa:F401 | ||
CategoricalIndex, DatetimeIndex, Float64Index, Index, Int64Index, | ||
IntervalIndex, InvalidIndexError, MultiIndex, NaT, NumericIndex, | ||
PeriodIndex, RangeIndex, TimedeltaIndex, UInt64Index, _all_indexes_same, | ||
_get_combined_index, _get_consensus_names, _get_objs_combined_axis, | ||
_new_Index, _union_indexes, ensure_index, ensure_index_from_sequences) | ||
from pandas.core.indexes.multi import _sparsify # noqa:F401 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
from collections import abc | ||
from datetime import datetime, time | ||
from functools import partial | ||
from typing import Optional, TypeVar, Union | ||
|
||
import numpy as np | ||
|
||
|
@@ -14,12 +15,25 @@ | |
from pandas.core.dtypes.common import ( | ||
ensure_object, is_datetime64_dtype, is_datetime64_ns_dtype, | ||
is_datetime64tz_dtype, is_float, is_integer, is_integer_dtype, | ||
is_list_like, is_numeric_dtype, is_object_dtype, is_scalar) | ||
from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries | ||
is_list_like, is_numeric_dtype, is_scalar) | ||
from pandas.core.dtypes.generic import ( | ||
ABCDataFrame, ABCDatetimeIndex, ABCIndex, ABCIndexClass, ABCSeries) | ||
from pandas.core.dtypes.missing import notna | ||
|
||
from pandas._typing import ArrayLike | ||
from pandas.core import algorithms | ||
|
||
# --------------------------------------------------------------------- | ||
# types used in annotations | ||
|
||
Scalar = Union[int, float, str] | ||
DatetimeScalar = TypeVar('DatetimeScalar', Scalar, datetime) | ||
DatetimeScalarOrArrayConvertible = Union[DatetimeScalar, list, tuple, | ||
anmyachev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ArrayLike, ABCSeries] | ||
|
||
|
||
# --------------------------------------------------------------------- | ||
|
||
|
||
def _guess_datetime_format_for_array(arr, **kwargs): | ||
# Try to guess the format based on the first non-NaN element | ||
|
@@ -60,7 +74,43 @@ def _maybe_cache(arg, format, cache, convert_listlike): | |
return cache_array | ||
|
||
|
||
def _convert_and_box_cache(arg, cache_array, box, errors, name=None): | ||
def _box_as_indexlike( | ||
dt_array: ArrayLike, | ||
utc: Optional[bool] = None, | ||
name: Optional[str] = None | ||
) -> Union[ABCIndex, ABCDatetimeIndex]: | ||
""" | ||
Properly boxes the ndarray of datetimes to DatetimeIndex | ||
if it is possible or to generic Index instead | ||
|
||
Parameters | ||
---------- | ||
dt_array: 1-d array | ||
array of datetimes to be boxed | ||
tz : object | ||
anmyachev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
None or 'utc' | ||
name : string, default None | ||
Name for a resulting index | ||
|
||
Returns | ||
------- | ||
result : datetime of converted dates | ||
- DatetimeIndex if convertible to sole datetime64 type | ||
- general Index otherwise | ||
""" | ||
from pandas import DatetimeIndex, Index | ||
if is_datetime64_dtype(dt_array): | ||
tz = 'utc' if utc else None | ||
return DatetimeIndex(dt_array, tz=tz, name=name) | ||
return Index(dt_array, name=name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to apply There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jreback @mroeschke After a close look at the Index constructor( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. its default is |
||
|
||
|
||
def _convert_and_box_cache( | ||
arg: DatetimeScalarOrArrayConvertible, | ||
cache_array: ABCSeries, | ||
box: bool, | ||
name: Optional[str] = None | ||
) -> Union[ABCIndex, np.ndarray]: | ||
""" | ||
Convert array of dates with a cache and box the result | ||
|
||
|
@@ -71,26 +121,19 @@ def _convert_and_box_cache(arg, cache_array, box, errors, name=None): | |
Cache of converted, unique dates | ||
box : boolean | ||
True boxes result as an Index-like, False returns an ndarray | ||
errors : string | ||
'ignore' plus box=True will convert result to Index | ||
name : string, default None | ||
Name for a DatetimeIndex | ||
|
||
Returns | ||
------- | ||
result : datetime of converted dates | ||
Returns: | ||
|
||
- Index-like if box=True | ||
- ndarray if box=False | ||
""" | ||
from pandas import Series, DatetimeIndex, Index | ||
from pandas import Series | ||
result = Series(arg).map(cache_array) | ||
if box: | ||
if errors == 'ignore': | ||
return Index(result, name=name) | ||
else: | ||
return DatetimeIndex(result, name=name) | ||
return _box_as_indexlike(result, utc=None, name=name) | ||
return result.values | ||
|
||
|
||
|
@@ -118,7 +161,6 @@ def _return_parsed_timezone_results(result, timezones, box, tz, name): | |
|
||
- Index-like if box=True | ||
- ndarray of Timestamps if box=False | ||
|
||
""" | ||
if tz is not None: | ||
raise ValueError("Cannot pass a tz argument when " | ||
|
@@ -324,13 +366,8 @@ def _convert_listlike_datetimes(arg, box, format, name=None, tz=None, | |
return np.array(result, dtype=object) | ||
|
||
if box: | ||
# Ensure we return an Index in all cases where box=True | ||
if is_datetime64_dtype(result): | ||
return DatetimeIndex(result, tz=tz, name=name) | ||
elif is_object_dtype(result): | ||
# e.g. an Index of datetime objects | ||
from pandas import Index | ||
return Index(result, name=name) | ||
utc = tz == 'utc' | ||
return _box_as_indexlike(result, utc=utc, name=name) | ||
return result | ||
|
||
|
||
|
@@ -611,15 +648,15 @@ def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False, | |
elif isinstance(arg, ABCIndexClass): | ||
cache_array = _maybe_cache(arg, format, cache, convert_listlike) | ||
if not cache_array.empty: | ||
result = _convert_and_box_cache(arg, cache_array, box, errors, | ||
result = _convert_and_box_cache(arg, cache_array, box, | ||
name=arg.name) | ||
else: | ||
convert_listlike = partial(convert_listlike, name=arg.name) | ||
result = convert_listlike(arg, box, format) | ||
elif is_list_like(arg): | ||
cache_array = _maybe_cache(arg, format, cache, convert_listlike) | ||
if not cache_array.empty: | ||
result = _convert_and_box_cache(arg, cache_array, box, errors) | ||
result = _convert_and_box_cache(arg, cache_array, box) | ||
else: | ||
result = convert_listlike(arg, box, format) | ||
else: | ||
|
Uh oh!
There was an error while loading. Please reload this page.