-
-
Notifications
You must be signed in to change notification settings - Fork 18k
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
DEPR: convert_datetime64 parameter in to_records() #18902
Conversation
doc/source/whatsnew/v0.23.0.txt
Outdated
@@ -216,6 +216,7 @@ Deprecations | |||
- ``Series.valid`` is deprecated. Use :meth:`Series.dropna` instead (:issue:`18800`). | |||
- :func:`read_excel` has deprecated the ``skip_footer`` parameter. Use ``skipfooter`` instead (:issue:`18836`) | |||
- The ``is_copy`` attribute is deprecated and will be removed in a future version (:issue:`18801`). | |||
- The ``convert_datetime64`` parameter has been deprecated and the default value is now ``False`` in :func:`to_records` as the NumPy bug motivating this parameter has been resolved (:issue:`18160`). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should :func:`to_records`
actually be :func:`DataFrame.to_records`
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-
@jschendel : I believe you are correct
-
@reidy-p : This sentence is a run-on. Break it up into two sentences (and add comma's at the conjunctions).
@reidy-p : I'm a little surprised to see that you didn't have to change any tests with this deprecation, as it is also an API change. If this passes, it means coverage for this function is not that great. Perhaps we should take the opportunity to write a few more tests... |
@gfyoung yeah I was surprised too. I'll take a look at the existing tests in more detail and see if I can add some more. |
Codecov Report
@@ Coverage Diff @@
## master #18902 +/- ##
==========================================
- Coverage 91.84% 91.81% -0.03%
==========================================
Files 153 153
Lines 49275 49277 +2
==========================================
- Hits 45255 45245 -10
- Misses 4020 4032 +12
Continue to review full report at Codecov.
|
8202f2c
to
6a31ce3
Compare
pandas/core/frame.py
Outdated
@@ -1187,7 +1187,7 @@ def from_records(cls, data, index=None, exclude=None, columns=None, | |||
|
|||
return cls(mgr) | |||
|
|||
def to_records(self, index=True, convert_datetime64=True): | |||
def to_records(self, index=True, convert_datetime64=False): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make the default value None
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@reidy-p did you see my comment here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I changed the default to None
but was then asked to change it to False
instead in a later review.
I think there are at least two options for handling the deprecation of the convert_datetime64
parameter:
-
Change default from
True
toNone
. Give a warning ifconvert_datetime64
is notNone
. Ifconvert_datetime64
isNone
then setconvert_datetime64=True
inside the function to avoid breaking existing code. -
Change default from
True
toFalse
. Give a warning ifconvert_datetime64=True
.
Which option is better?
@jorisvandenbossche I think you recommended Option 1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes I think we need to make this None
, then can detect that a user is passing this. Yes we are also completely changing the behavior of this, but we really want to remove this parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is in-line with @jorisvandenbossche comments. We want to warn if a user is passing this at ALL, and we are changing the default. (so its now False
if the parameter is not specified). can you reword as appropriate. ping when ready.
pandas/core/frame.py
Outdated
@@ -1196,14 +1196,23 @@ def to_records(self, index=True, convert_datetime64=True): | |||
---------- | |||
index : boolean, default True | |||
Include index in resulting record array, stored in 'index' field | |||
convert_datetime64 : boolean, default True | |||
convert_datetime64 : boolean, default False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
boolean, optional
pandas/core/frame.py
Outdated
Whether to convert the index to datetime.datetime if it is a | ||
DatetimeIndex | ||
|
||
Returns | ||
------- | ||
y : recarray | ||
""" | ||
|
||
if convert_datetime64: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is not None. We are going to completely remove this parameter, so we want to warn if its passed at all.
with tm.assert_produces_warning(FutureWarning): | ||
expected = df.index[0] | ||
result = df.to_records(convert_datetime64=True)['index'][0] | ||
assert expected == result | ||
|
||
rs = df.to_records(convert_datetime64=False) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you will need to catch the warning on the following as well
doc/source/whatsnew/v0.23.0.txt
Outdated
@@ -216,6 +216,7 @@ Deprecations | |||
- ``Series.valid`` is deprecated. Use :meth:`Series.dropna` instead (:issue:`18800`). | |||
- :func:`read_excel` has deprecated the ``skip_footer`` parameter. Use ``skipfooter`` instead (:issue:`18836`) | |||
- The ``is_copy`` attribute is deprecated and will be removed in a future version (:issue:`18801`). | |||
- The ``convert_datetime64`` parameter in :func:`DataFrame.to_records` has been deprecated and the default value is now ``False``. The NumPy bug motivating this parameter has been resolved (:issue:`18160`). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
say it will be removed
5b4bc9c
to
cb0dfd5
Compare
pandas/core/frame.py
Outdated
@@ -1196,14 +1196,23 @@ def to_records(self, index=True, convert_datetime64=True): | |||
---------- | |||
index : boolean, default True | |||
Include index in resulting record array, stored in 'index' field | |||
convert_datetime64 : boolean, default True | |||
convert_datetime64 : boolean, optional | |||
.. deprecated:: 0.23.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right, but it still defaults to True (its just we are getting rid of it).
"deprecated and will be removed in a future " | ||
"version", | ||
FutureWarning, stacklevel=2) | ||
|
||
if index: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
else:
convert_datetime64 = True
|
||
rs = df.to_records(convert_datetime64=False) | ||
assert rs['index'][0] == df.index.values[0] | ||
with tm.assert_produces_warning(FutureWarning): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add an example where we don't pass it, so it gets defaulted to True.
c13b109
to
ef6cf9b
Compare
ef6cf9b
to
00b10db
Compare
pandas/core/frame.py
Outdated
Whether to convert the index to datetime.datetime if it is a | ||
DatetimeIndex | ||
|
||
Returns | ||
------- | ||
y : recarray | ||
""" | ||
|
||
if convert_datetime64 is not None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jorisvandenbossche how should we handle this here. We really want to change the default to False in the future, but need True to make this backwards compat for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
on 2nd thought, let's just change this to default to False
. We have to change it now or later anyways.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to have to None
as a default to detect the case that people now did convert_datetime64=False
explicitly. Otherwise they don't see the deprecation warning, and their code will break once we remove the keyword.
can you rebase |
d9c9d7c
to
eca4757
Compare
pandas/core/frame.py
Outdated
Whether to convert the index to datetime.datetime if it is a | ||
DatetimeIndex | ||
|
||
Returns | ||
------- | ||
y : recarray | ||
""" | ||
|
||
if convert_datetime64 is not None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
on 2nd thought, let's just change this to default to False
. We have to change it now or later anyways.
result = df.to_records(convert_datetime64=True)['index'][0] | ||
assert expected == result | ||
|
||
expected = df.index[0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move comment above expected
assert expected == result | ||
|
||
with tm.assert_produces_warning(FutureWarning): | ||
rs = df.to_records(convert_datetime64=False) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you test with True as well
eca4757
to
94ee32b
Compare
94ee32b
to
32606c5
Compare
32606c5
to
f4c6190
Compare
(moving this to the main thread) Which option is best depends on what the end goal here is. Given the deprecation warning, it seems we actually want to remove it. So I think we need a default of None (and then best give a different warning in case True or False was specified). But having a default of None, does not mean that if it is None, it should mean We could also delay the actual change in behaviour and first warn that it will change in the future (your option 1). |
eb0b207
to
fd0f114
Compare
pandas/core/frame.py
Outdated
@@ -1187,7 +1187,7 @@ def from_records(cls, data, index=None, exclude=None, columns=None, | |||
|
|||
return cls(mgr) | |||
|
|||
def to_records(self, index=True, convert_datetime64=True): | |||
def to_records(self, index=True, convert_datetime64=False): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes I think we need to make this None
, then can detect that a user is passing this. Yes we are also completely changing the behavior of this, but we really want to remove this parameter.
pandas/core/frame.py
Outdated
@@ -1187,7 +1187,7 @@ def from_records(cls, data, index=None, exclude=None, columns=None, | |||
|
|||
return cls(mgr) | |||
|
|||
def to_records(self, index=True, convert_datetime64=True): | |||
def to_records(self, index=True, convert_datetime64=False): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is in-line with @jorisvandenbossche comments. We want to warn if a user is passing this at ALL, and we are changing the default. (so its now False
if the parameter is not specified). can you reword as appropriate. ping when ready.
if you can update @reidy-p |
b0cc2be
to
42d75e0
Compare
42d75e0
to
4a1b1ba
Compare
@jreback I think this is ready but would be good if you could double-check the tests to make sure they make sense. |
lgtm. thanks @reidy-p |
git diff upstream/master -u -- "*.py" | flake8 --diff
As noted in the original issue, the underlying NumPy bug seems to be fixed so this parameter may no longer be needed.
I also changed the default value for
convert_datetime64
fromTrue
toFalse
and only give aFutureWarning
whenconvert_datetime64=True
is passed but I'm not sure if this is more appropriate than leavingTrue
as the default and always giving aFutureWarning
.