-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: concat/append misc fixes #13660
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
Merged
Changes from all commits
Commits
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
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
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
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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import nose | ||
import pandas as pd | ||
import pandas.types.concat as _concat | ||
import pandas.util.testing as tm | ||
|
||
|
||
class TestConcatCompat(tm.TestCase): | ||
|
||
_multiprocess_can_split_ = True | ||
|
||
def check_concat(self, to_concat, exp): | ||
for klass in [pd.Index, pd.Series]: | ||
to_concat_klass = [klass(c) for c in to_concat] | ||
res = _concat.get_dtype_kinds(to_concat_klass) | ||
self.assertEqual(res, set(exp)) | ||
|
||
def test_get_dtype_kinds(self): | ||
to_concat = [['a'], [1, 2]] | ||
self.check_concat(to_concat, ['i', 'object']) | ||
|
||
to_concat = [[3, 4], [1, 2]] | ||
self.check_concat(to_concat, ['i']) | ||
|
||
to_concat = [[3, 4], [1, 2.1]] | ||
self.check_concat(to_concat, ['i', 'f']) | ||
|
||
def test_get_dtype_kinds_datetimelike(self): | ||
to_concat = [pd.DatetimeIndex(['2011-01-01']), | ||
pd.DatetimeIndex(['2011-01-02'])] | ||
self.check_concat(to_concat, ['datetime']) | ||
|
||
to_concat = [pd.TimedeltaIndex(['1 days']), | ||
pd.TimedeltaIndex(['2 days'])] | ||
self.check_concat(to_concat, ['timedelta']) | ||
|
||
def test_get_dtype_kinds_datetimelike_object(self): | ||
to_concat = [pd.DatetimeIndex(['2011-01-01']), | ||
pd.DatetimeIndex(['2011-01-02'], tz='US/Eastern')] | ||
self.check_concat(to_concat, | ||
['datetime', 'datetime64[ns, US/Eastern]']) | ||
|
||
to_concat = [pd.DatetimeIndex(['2011-01-01'], tz='Asia/Tokyo'), | ||
pd.DatetimeIndex(['2011-01-02'], tz='US/Eastern')] | ||
self.check_concat(to_concat, | ||
['datetime64[ns, Asia/Tokyo]', | ||
'datetime64[ns, US/Eastern]']) | ||
|
||
# timedelta has single type | ||
to_concat = [pd.TimedeltaIndex(['1 days']), | ||
pd.TimedeltaIndex(['2 hours'])] | ||
self.check_concat(to_concat, ['timedelta']) | ||
|
||
to_concat = [pd.DatetimeIndex(['2011-01-01'], tz='Asia/Tokyo'), | ||
pd.TimedeltaIndex(['1 days'])] | ||
self.check_concat(to_concat, | ||
['datetime64[ns, Asia/Tokyo]', 'timedelta']) | ||
|
||
def test_get_dtype_kinds_period(self): | ||
# because we don't have Period dtype (yet), | ||
# Series results in object dtype | ||
to_concat = [pd.PeriodIndex(['2011-01'], freq='M'), | ||
pd.PeriodIndex(['2011-01'], freq='M')] | ||
res = _concat.get_dtype_kinds(to_concat) | ||
self.assertEqual(res, set(['period[M]'])) | ||
|
||
to_concat = [pd.Series([pd.Period('2011-01', freq='M')]), | ||
pd.Series([pd.Period('2011-02', freq='M')])] | ||
res = _concat.get_dtype_kinds(to_concat) | ||
self.assertEqual(res, set(['object'])) | ||
|
||
to_concat = [pd.PeriodIndex(['2011-01'], freq='M'), | ||
pd.PeriodIndex(['2011-01'], freq='D')] | ||
res = _concat.get_dtype_kinds(to_concat) | ||
self.assertEqual(res, set(['period[M]', 'period[D]'])) | ||
|
||
to_concat = [pd.Series([pd.Period('2011-01', freq='M')]), | ||
pd.Series([pd.Period('2011-02', freq='D')])] | ||
res = _concat.get_dtype_kinds(to_concat) | ||
self.assertEqual(res, set(['object'])) | ||
|
||
|
||
if __name__ == '__main__': | ||
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'], | ||
exit=False) |
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
Oops, something went wrong.
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.
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.
(a bit similar with the
equals
case)Currently, we are accepting a list or tuple to be added (in case that it is a list of list, in the flat case you get an error):
The docstring of append also says for
other
: "Index or list/tuple of indices"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.
_ensure_index
should handle this (it might think its aMultiIndex
actually), so might need to disambiguateThere 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,
_ensure_index
would convert list to Index. @jreback are you sayingIndex.append
should accept lists?On second thought, maybe I misread the
Index.append
docstring. The "Index or list/tuple of indices" can be interpreted as both as "list/tuple of Index objects" (so no guarantee to accept list as Index object) or "list of index labels".But actually probably the first interpretation. In that case, I think this PR is OK, but @sinhrks it would maybe be good to list this in the whatsnew as change as well? (or do we regard it as a bug fix?)
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 agree docstring intends first one. I'm adding an validation to check all elements are
Index
if input is list-like.I think it is regarded as a bug fix, because it is similar to the fix which
CategiricalIndex.append
accepts (flat) list of labels (included in this PR).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, merging then!