-
-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: deprecate .asobject property #18572
DEPR: deprecate .asobject property #18572
Conversation
dbe3323
to
4cb8776
Compare
Codecov Report
@@ Coverage Diff @@
## master #18572 +/- ##
==========================================
- Coverage 91.35% 91.33% -0.02%
==========================================
Files 164 164
Lines 49802 49806 +4
==========================================
- Hits 45496 45490 -6
- Misses 4306 4316 +10
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #18572 +/- ##
==========================================
- Coverage 91.46% 91.42% -0.05%
==========================================
Files 157 157
Lines 51449 51454 +5
==========================================
- Hits 47060 47043 -17
- Misses 4389 4411 +22
Continue to review full report at Codecov.
|
all is green, I think the failure on appveyoyor is unrelated to the PR. Some points:
|
In general: please don't open new PRs, but just update the original one. |
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.
Looks good to me!
One comment on the deprecation message
pandas/core/series.py
Outdated
@@ -420,12 +420,15 @@ def get_values(self): | |||
|
|||
@property | |||
def asobject(self): | |||
""" | |||
"""DEPRECATED: Use ``astype(object).values`` instead. |
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 know "astype(object).values" is the exact replacement, but I would maybe still recommend just "astype(object)". In many cases a Series will just be fine as well, and if the user really wants the array they can do an additional .values
.
I just want to prevent people using .values
when it is not needed. Maybe we can mention both as well, although that makes the warning a bit long.
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.
don't add .values
, that is just adding even more confusion.
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.
Ok, recommend to use astype(object)
, but still return .astype(object).values
, so the API doesn't break.
@@ -51,15 +51,15 @@ def test_ops_properties_basic(self): | |||
assert s.day == 10 | |||
pytest.raises(AttributeError, lambda: s.weekday) | |||
|
|||
def test_asobject_tolist(self): | |||
def test_astype(self): |
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.
maybe test_astype_object
? (if it is only testing that)
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.
need to completely remove _asobject
and replace with .astype(object)
, this may require some small changes in def astype(...)
on the Index subclasses.
w/o changing this we are just adding even more technical debt.
doc/source/whatsnew/v0.22.0.txt
Outdated
@@ -89,7 +89,8 @@ Deprecations | |||
|
|||
- ``Series.from_array`` and ``SparseSeries.from_array`` are deprecated. Use the normal constructor ``Series(..)`` and ``SparseSeries(..)`` instead (:issue:`18213`). | |||
- ``DataFrame.as_matrix`` is deprecated. Use ``DataFrame.values`` instead (:issue:`18458`). | |||
- | |||
- ``DatetimeIndex.asobject``, ``PeriodIndex.asobject`` and ``TimeDeltaIndex.asobject`` have been deprecated. Use '.astype(object)' instead (:issue:`18572`) | |||
- ``Series.asobject`` has been deprecated. Use '.astype(object).values' instead (:issue:`18572`) |
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.
no need to suggest .values
pandas/core/indexes/datetimelike.py
Outdated
@@ -421,15 +421,25 @@ def _isnan(self): | |||
return (self.asi8 == iNaT) | |||
|
|||
@property | |||
def asobject(self): |
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 actually want to completely remove _asobject
, this just adds even more technical debt.
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.
Jeff, is it used in astype
for both DatetimeIndex
, PeriodIndex
and TimedeltaIndex
, so it is fine to have it like this, otherwise this line would be duplicated in all three of them.
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.
so it is fine to have it like this,
is it used in astype for both DatetimeIndex, PeriodIndex and TimedeltaIndex,
and that should be addressed as well.
no its not. much better to fix this. otherwise this is just kicking the can.
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.
If this method is only used there, what is the problem? Then it is a perfect example of a common private helper method that our classes are full of.
much better to fix this.
Then please indicate what you mean with "fix"
PR updated and all is green now. Wrt. Changing it later would be trivial, as its only called from three places. so IMO its not a very large technical debt, if it even is one. |
ok, do this move what you are calling |
@@ -552,8 +552,8 @@ cpdef ensure_object(object arr): | |||
return arr | |||
else: | |||
return arr.astype(np.object_) | |||
elif hasattr(arr, 'asobject'): | |||
return arr.asobject |
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.
see what effect of removing this does
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.
Two tests fail, both at pandas\tests\test_categorical.py:811
(parameterized tests, of categorical of dtype='timedelta64[h]' and is_ordered=True).
6e7894a
to
656534e
Compare
Updated.
|
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.
small change, otherwise lgtm.
pandas/tests/indexes/datetimelike.py
Outdated
@@ -75,3 +75,17 @@ def test_map_dictlike(self, mapper): | |||
expected = pd.Index([pd.NaT] * len(self.index)) | |||
result = self.index.map(mapper([], [])) | |||
tm.assert_index_equal(result, expected) | |||
|
|||
def test_asobject_deprecated(self): | |||
d = pd.date_range('2010-01-1', periods=3) |
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 the issue number as a comment
use d = self.create_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.
Ok, uploaded. self.create_index()
takes care of calling the correct index type?
7c9ccc7
to
ad263d9
Compare
All green. Please verify if last change is ok. |
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.
needs a rebase and a change. otherwise lgtm.
pandas/core/indexes/datetimelike.py
Outdated
@@ -242,6 +242,14 @@ def _box_values(self, values): | |||
""" | |||
return lib.map_infer(values, self._box_func) | |||
|
|||
@property |
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 this a function,
f93f999
to
a610a0c
Compare
All green. |
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.
lgtm. ping on green.
pandas/tests/indexes/datetimelike.py
Outdated
def test_asobject_deprecated(self): | ||
# GH18572 | ||
d = self.create_index() | ||
print(d) |
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.
remove this print
pandas/tests/indexes/datetimelike.py
Outdated
d = self.create_index() | ||
print(d) | ||
with tm.assert_produces_warning(FutureWarning): | ||
i = d.asobject |
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 can do the assert inside the with
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.
print here as well
@@ -290,8 +290,9 @@ def test_comp_nat(self): | |||
pd.Period('2011-01-03')]) | |||
right = pd.PeriodIndex([pd.NaT, pd.NaT, pd.Period('2011-01-03')]) | |||
|
|||
for l, r in [(left, right), (left.asobject, right.asobject)]: | |||
result = l == r | |||
for l, r in [(left, right), |
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 name these lhs, rhs
instead of l, r
(to avoid future flake warning)
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.
ok.
def test_asobject_deprecated(self): | ||
s = Series(np.random.randn(5), name='foo') | ||
with tm.assert_produces_warning(FutureWarning): | ||
o = s.asobject |
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 can assert inside the with
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 don't see why: by having it outside, we're clear that the with statement is about the call s.asobject
and not the call's return value.
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.
to be honest I actually don't care about the return value here, this is adequately tested elsewhere, the point of this test is just to assert the warning.
a610a0c
to
f1ad983
Compare
Hello @topper-123! Thanks for updating the PR. Cheers ! There are no PEP8 issues in this Pull Request. 🍻 Comment last updated on December 04, 2017 at 10:00 Hours UTC |
16583a5
to
d61f411
Compare
Thanks a lot @topper-123 ! |
This PR supersedes #18477.
closes #18237
git diff upstream/master -u -- "*.py" | flake8 --diff
Deprecate
Series.asobject
andDatetimeIndexOpsMixin.asobject
as per discussion in #18262.DatetimeIndexOpsMixin
is a mixin forDatetimeIndex
,PeriodIndex
andTimeDeltaIndex
, so all the.asobject
property will be deprecated all those classes.Internal references to
asobject
have been cleaned up, so a eventual removal will be easy when that time comes.