8000 Concatenate with sequence that contains empty sequences. (Trac #988) · Issue #1586 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Concatenate with sequence that contains empty sequences. (Trac #988) #1586

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

Closed
thouis opened this issue Oct 19, 2012 · 12 comments
Closed

Concatenate with sequence that contains empty sequences. (Trac #988) #1586

thouis opened this issue Oct 19, 2012 · 12 comments

Comments

@thouis
Copy link
Contributor
thouis commented Oct 19, 2012

Original ticket http://projects.scipy.org/numpy/ticket/988 on 2009-01-22 by trac user kelson, assigned to unknown.

Howdy,

After searching for this defect, perhaps nobody else has been bothered
by this behaviour, but has anybody else noticed:

concatenate ( [[1],[2],[3],[4]] ).dtype = int32

concatenate ( [[1],[2],[3],[]] ).dtype = int32

concatenate ( [[],[2],[3],[4]] ).dtype = float64

I have a lot of old-style Numeric stuff that likes to concatenate lists
of indices which then were used with take [which does not like floats]. So
below [in theory] I attach my attempt at a patch so that empty items in the sequence are
not used to influence the common type that is used by PyArray_ConvertToCommonType.

Maybe it is of some value, or maybe I didn't do this in a kosher manner and
someone else has an idea of where things should be rethought for this defect?

Cheerio.

@thouis
Copy link
Contributor Author
thouis commented Oct 19, 2012

Attachment added by trac user kelson on 2009-01-22: numpy121b.patch

@thouis
Copy link
Contributor Author
thouis commented Oct 19, 2012

@charris wrote on 2009-04-11

The patch is not a unified diff and can't be viewed on trac. It is also against an outdated version. It needs a closer look at the original function to see if it is good.

Apart from that, I think the current behavior should be fixed, but maybe this isn't the best fix. It needs some thought.

@thouis
Copy link
Contributor Author
thouis commented Oct 19, 2012

@charris wrote on 2009-04-11

It's a context diff and only one of the four chunks apply.

@thouis
Copy link
Contributor Author
thouis commented Oct 19, 2012

@mwiebe wrote on 2011-03-24

This behavior is still in 1.6 beta.

@thouis
Copy link
Contributor Author
thouis commented Oct 19, 2012

Milestone changed to Unscheduled by @mwiebe on 2011-03-24

@charris
Copy link
Member
charris commented Feb 18, 2014

Still here in 1.9-devel

In [89]: concatenate ( [[1],[2],[3],[4]] ).dtype
Out[89]: dtype('int64')

In [90]: concatenate ( [[1],[2],[3],[]] ).dtype
Out[90]: dtype('float64')

jaimefrio added a commit to jaimefrio/numpy that referenced this issue Aug 20, 2015
jaimefrio added a commit to jaimefrio/numpy that referenced this issue Mar 22, 2016
Empty non-arrays no longer participate in determining the dtype of
the concatenated array. Have also refactored the code to unify as
much as possible the logic for flattened and non-flattened paths,
unifying the error checks and adding a few more tests, both for
the fixed bug and for other functionality.
jaimefrio added a commit to jaimefrio/numpy that referenced this issue Apr 3, 2017
Empty non-arrays no longer participate in determining the dtype of
the result.

Have also refactored the code quite a bit, unifying the flattened
and multidim branches wherever possible, as well as the error checks.
Also a handful of new tests, including both the bug fixed and
existing untested functionality.
@eric-wieser
Copy link
Member

On master, concatenate ( [[1],[2],[3],[]] ).dtype changed from int32, to float64

@WGautier
Copy link
WGautier commented Sep 12, 2018

Still seeing this on 1.15.1.

>>> np.version.version
'1.15.1'
>>> np.concatenate(([],[1])).dtype
dtype('float64')
>>> np.concatenate(([2],[1])).dtype
dtype('int64')

@eric-wieser
Copy link
Member
eric-wieser commented Sep 12, 2018

In my opinion, returning float64 is correct, because np.array([]).dtype = float64

@WGautier
Copy link
WGautier commented Sep 12, 2018

Technically true, but it's not the most intuitive result from a simple concatenate function.

I will work around it, thanks for the quick answer.

@eric-wieser
Copy link
Member

The best way to avoid this kind of problem is to work with arrays rather than lists where possible, and specify explicit dtypes in the few times when you need to convert.

@mattip
Copy link
Member
mattip commented Apr 30, 2019

Closing

@mattip mattip closed this as completed Apr 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants
0