-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
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 1 commit
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
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,17 +60,15 @@ def _maybe_cache(arg, format, cache, convert_listlike): | |
return cache_array | ||
|
||
|
||
def _box_if_needed(dt_array, box, default, tz, name=None): | ||
def _box_as_indexlike(dt_array, tz=None, name=None): | ||
""" | ||
Properly boxes the ndarray of datetimes (if requested) to DatetimeIndex | ||
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 | ||
box : boolean | ||
True boxes result as an Index-like, False returns an ndarray | ||
tz : object | ||
None or 'utc' | ||
name : string, default None | ||
|
@@ -79,18 +77,13 @@ def _box_if_needed(dt_array, box, default, tz, name=None): | |
Returns | ||
------- | ||
result : datetime of converted dates | ||
Returns: | ||
|
||
- Index-like if box=True | ||
- ndarray if box=False | ||
- DatetimeIndex if convertible to sole datetime64 type | ||
- general Index otherwise | ||
""" | ||
if box: | ||
from pandas import DatetimeIndex, Index | ||
if is_datetime64_dtype(dt_array): | ||
return DatetimeIndex(dt_array, tz=tz, name=name) | ||
# e.g. an Index of datetime objects | ||
return Index(dt_array, name=name) | ||
return default | ||
from pandas import DatetimeIndex, Index | ||
if is_datetime64_dtype(dt_array): | ||
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, cache_array, box, name=None): | ||
anmyachev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
@@ -117,7 +110,9 @@ def _convert_and_box_cache(arg, cache_array, box, name=None): | |
""" | ||
from pandas import Series | ||
result = Series(arg).map(cache_array) | ||
return _box_if_needed(result, box, result.values, None, name) | ||
if box: | ||
return _box_as_indexlike(result, tz=None, 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. Do we have a test where 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. @anmyachev do we have a test like this? you are passing tz in the call below 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 yes. For example, @pytest.mark.parametrize('cache', [True, False])
def test_to_datetime_tz_pytz(self, cache):
# see gh-8260
us_eastern = pytz.timezone('US/Eastern')
arr = np.array([us_eastern.localize(datetime(year=2000, month=1, day=1,
hour=3, minute=0)),
us_eastern.localize(datetime(year=2000, month=6, day=1,
hour=3, minute=0))],
dtype=object)
result = pd.to_datetime(arr, utc=True, cache=cache)
expected = DatetimeIndex(['2000-01-01 08:00:00+00:00',
'2000-06-01 07:00:00+00:00'],
dtype='datetime64[ns, UTC]', freq=None)
tm.assert_index_equal(result, expected) 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. I c, we call the passed parameter tz, which is sligthly confusing. |
||
return result.values | ||
|
||
|
||
def _return_parsed_timezone_results(result, timezones, box, tz, name): | ||
|
@@ -349,7 +344,9 @@ def _convert_listlike_datetimes(arg, box, format, name=None, tz=None, | |
for ts in result] | ||
return np.array(result, dtype=object) | ||
|
||
return _box_if_needed(result, box, result, tz, name) | ||
if box: | ||
return _box_as_indexlike(result, tz=tz, name=name) | ||
return result | ||
|
||
|
||
def _adjust_to_origin(arg, origin, unit): | ||
|
Uh oh!
There was an error while loading. Please reload this page.