-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
DEPR: Series/DataFrame with tzaware data and tznaive dtype #41555
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
Merged
jreback
merged 3 commits into
pandas-dev:master
from
jbrockmendel:ref-maybe_cast_to_datetime
May 21, 2021
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -219,6 +219,8 @@ def maybe_unbox_datetimelike(value: Scalar, dtype: DtypeObj) -> Scalar: | |
elif isinstance(value, Timestamp): | ||
if value.tz is None: | ||
value = value.to_datetime64() | ||
elif not isinstance(dtype, DatetimeTZDtype): | ||
raise TypeError("Cannot unbox tzaware Timestamp to tznaive dtype") | ||
elif isinstance(value, Timedelta): | ||
value = value.to_timedelta64() | ||
|
||
|
@@ -1616,9 +1618,21 @@ def maybe_cast_to_datetime( | |
# didn't specify one | ||
|
||
if dta.tz is not None: | ||
warnings.warn( | ||
"Data is timezone-aware. Converting " | ||
"timezone-aware data to timezone-naive by " | ||
"passing dtype='datetime64[ns]' to " | ||
"DataFrame or Series is deprecated and will " | ||
"raise in a future version. Use " | ||
"`pd.Series(values).dt.tz_localize(None)` " | ||
"instead.", | ||
FutureWarning, | ||
stacklevel=8, | ||
) | ||
# equiv: dta.view(dtype) | ||
# Note: NOT equivalent to dta.astype(dtype) | ||
dta = dta.tz_localize(None) | ||
|
||
value = dta | ||
elif is_datetime64tz: | ||
dtype = cast(DatetimeTZDtype, dtype) | ||
|
@@ -1810,7 +1824,7 @@ def construct_2d_arraylike_from_scalar( | |
shape = (length, width) | ||
|
||
if dtype.kind in ["m", "M"]: | ||
value = maybe_unbox_datetimelike(value, dtype) | ||
value = maybe_unbox_datetimelike_tz_deprecation(value, dtype, stacklevel=4) | ||
elif dtype == object: | ||
if isinstance(value, (np.timedelta64, np.datetime64)): | ||
# calling np.array below would cast to pytimedelta/pydatetime | ||
|
@@ -1873,14 +1887,48 @@ def construct_1d_arraylike_from_scalar( | |
if not isna(value): | ||
value = ensure_str(value) | ||
elif dtype.kind in ["M", "m"]: | ||
value = maybe_unbox_datetimelike(value, dtype) | ||
value = maybe_unbox_datetimelike_tz_deprecation(value, dtype) | ||
|
||
subarr = np.empty(length, dtype=dtype) | ||
subarr.fill(value) | ||
|
||
return subarr | ||
|
||
|
||
def maybe_unbox_datetimelike_tz_deprecation( | ||
value: Scalar, dtype: DtypeObj, stacklevel: int = 5 | ||
): | ||
""" | ||
Wrap maybe_unbox_datetimelike with a check for a timezone-aware Timestamp | ||
along with a timezone-naive datetime64 dtype, which is deprecated. | ||
""" | ||
# Caller is responsible for checking dtype.kind in ["m", "M"] | ||
try: | ||
value = maybe_unbox_datetimelike(value, dtype) | ||
except TypeError: | ||
if ( | ||
isinstance(value, Timestamp) | ||
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. does this apply to a datetime as well? 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. good catch, will update, along with test |
||
and value.tz is not None | ||
and isinstance(dtype, np.dtype) | ||
): | ||
warnings.warn( | ||
"Data is timezone-aware. Converting " | ||
"timezone-aware data to timezone-naive by " | ||
"passing dtype='datetime64[ns]' to " | ||
"DataFrame or Series is deprecated and will " | ||
"raise in a future version. Use " | ||
"`pd.Series(values).dt.tz_localize(None)` " | ||
"instead.", | ||
FutureWarning, | ||
stacklevel=stacklevel, | ||
) | ||
new_value = value.tz_localize(None) | ||
return maybe_unbox_datetimelike(new_value, dtype) | ||
else: | ||
raise | ||
return value | ||
|
||
|
||
def construct_1d_object_array_from_listlike(values: Sized) -> np.ndarray: | ||
""" | ||
Transform any list-like object in a 1-dimensional numpy array of object | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.