-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Construct 1d array from listlike #18769
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 all commits
d4d02a2
0afff94
f5f3773
44eedfa
0892280
c4e635f
d0a6e48
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,3 +81,15 @@ def setup(self, nrows): | |
def time_frame_from_records_generator(self, nrows): | ||
# issue-6700 | ||
self.df = DataFrame.from_records(self.gen, nrows=nrows) | ||
|
||
|
||
class FromNDArray(object): | ||
|
||
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. are there already list-like constructor asv's here? and from dict? 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. no idea (unrelated to this PR) 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. and the answer is? 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. no idea, as above 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. well since you are adding this one, should be very easy to .parametrize and add similar to the Series case, at least a few simple ones. 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. Sure, some simple ones are present in 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. yes 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. |
||
goal_time = 0.2 | ||
|
||
def setup(self): | ||
N = 100000 | ||
self.data = np.random.randn(N) | ||
|
||
def time_frame_from_ndarray(self): | ||
self.df = DataFrame(self.data) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
from pandas.core.dtypes.missing import isna, isnull, notnull # noqa | ||
from pandas.api import types | ||
from pandas.core.dtypes import common | ||
from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike | ||
|
||
# compat | ||
from pandas.errors import ( # noqa | ||
|
@@ -381,25 +382,18 @@ def _asarray_tuplesafe(values, dtype=None): | |
return values.values | ||
|
||
if isinstance(values, list) and dtype in [np.object_, object]: | ||
return lib.list_to_object_array(values) | ||
return construct_1d_object_array_from_listlike(values) | ||
|
||
result = np.asarray(values, dtype=dtype) | ||
|
||
if issubclass(result.dtype.type, compat.string_types): | ||
result = np.asarray(values, dtype=object) | ||
|
||
if result.ndim == 2: | ||
if isinstance(values, list): | ||
return lib.list_to_object_array(values) | ||
else: | ||
# Making a 1D array that safely contains tuples is a bit tricky | ||
# in numpy, leading to the following | ||
try: | ||
result = np.empty(len(values), dtype=object) | ||
result[:] = values | ||
except ValueError: | ||
# we have a list-of-list | ||
result[:] = [tuple(x) for x in values] | ||
# Avoid building an array of arrays: | ||
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. is there an asv that hits this case? 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 don't even think there is any valid code path that hits this case... which indeed should be suppressed in #18626 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. if there is not valid code, then let's remove it. or make a new issue. inside a PR doesn't help. 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. 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. can you add the issue number here with a TODO 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. (done) |
||
# TODO: verify whether any path hits this except #18819 (invalid) | ||
values = [tuple(x) for x in values] | ||
result = construct_1d_object_array_from_listlike(values) | ||
|
||
return result | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,7 @@ def maybe_convert_platform(values): | |
""" try to do platform conversion, allow ndarray or list here """ | ||
|
||
if isinstance(values, (list, tuple)): | ||
values = lib.list_to_object_array(list(values)) | ||
values = construct_1d_object_array_from_listlike(list(values)) | ||
if getattr(values, 'dtype', None) == np.object_: | ||
if hasattr(values, '_values'): | ||
values = values._values | ||
|
@@ -1162,3 +1162,28 @@ def construct_1d_arraylike_from_scalar(value, length, dtype): | |
subarr.fill(value) | ||
|
||
return subarr | ||
|
||
|
||
def construct_1d_object_array_from_listlike(values): | ||
""" | ||
Transform any list-like object in a 1-dimensional numpy array of object | ||
dtype. | ||
|
||
Parameters | ||
---------- | ||
values : any iterable which has a len() | ||
|
||
Raises | ||
------ | ||
TypeError | ||
* If `values` does not have a len() | ||
|
||
Returns | ||
------- | ||
1-dimensional numpy array of dtype object | ||
""" | ||
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. would not object to an 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. The thing is: I can't think of any code path which could be hitting it. Scalar input to a 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. not what i am asking this is a completely internal 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.
Sure it does, 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. add a raises section to the doc-string |
||
# numpy will try to interpret nested lists as further dimensions, hence | ||
# making a 1D array that contains list-likes is a bit tricky: | ||
result = np.empty(len(values), dtype='object') | ||
result[:] = values | ||
return result |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,8 @@ | |
infer_dtype_from_array, | ||
maybe_convert_string_to_object, | ||
maybe_convert_scalar, | ||
find_common_type) | ||
find_common_type, | ||
construct_1d_object_array_from_listlike) | ||
from pandas.core.dtypes.dtypes import ( | ||
CategoricalDtype, | ||
DatetimeTZDtype, | ||
|
@@ -407,3 +408,17 @@ def test_period_dtype(self): | |
np.dtype('datetime64[ns]'), np.object, np.int64]: | ||
assert find_common_type([dtype, dtype2]) == np.object | ||
assert find_common_type([dtype2, dtype]) == np.object | ||
|
||
@pytest.mark.parametrize('datum1', [1, 2., "3", (4, 5), [6, 7], None]) | ||
@pytest.mark.parametrize('datum2', [8, 9., "10", (11, 12), [13, 14], None]) | ||
def test_cast_1d_array(self, datum1, datum2): | ||
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. are there fail cases? iow where this routine raises? 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. Not once you remove support for non- (well, as long as 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. right so can you add a test that fails for scalars then 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. (done) |
||
data = [datum1, datum2] | ||
result = construct_1d_object_array_from_listlike(data) | ||
|
||
# Direct comparison fails: https://github.com/numpy/numpy/issues/10218 | ||
assert result.dtype == 'object' | ||
assert list(result) == data | ||
|
||
@pytest.mark.parametrize('val', [1, 2., None]) | ||
def test_cast_1d_array_invalid_scalar(self, val): | ||
pytest.raises(TypeError, construct_1d_object_array_from_listlike, val) |
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.
some args are duplicated 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.
?
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.
sorry, was reading the ( as [