10000 BUG: Fix misuse of .names and .fields in various places (backport 14290 to 1.17) by eric-wieser · Pull Request #14339 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Fix misuse of .names and .fields in various places (backport 14290 to 1.17) #14339

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 13 commits into from
Aug 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
BUG: Don't allow extra fields to be added in genfromtxt
Previously passing `dtype=[], names=['a']` would append an extra field, even though `dtype=['a'], names=['b', 'c']` does not.
  • Loading branch information
eric-wieser committed Aug 20, 2019
commit b11468e1a38c19a24ed0ebaede023c159c745e33
11 changes: 5 additions & 6 deletions numpy/lib/_iotools.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,28 +925,27 @@ def easy_dtype(ndtype, names=None, defaultfmt="f%i", **validationargs):
names = validate(names, nbfields=nbfields, defaultfmt=defaultfmt)
ndtype = np.dtype(dict(formats=ndtype, names=names))
else:
nbtypes = len(ndtype)
# Explicit names
if names is not None:
validate = NameValidator(**validationargs)
if isinstance(names, basestring):
names = names.split(",")
# Simple dtype: repeat to match the nb of names
if nbtypes == 0:
if ndtype.names is None:
formats = tuple([ndtype.type] * len(names))
names = validate(names, defaultfmt=defaultfmt)
ndtype = np.dtype(list(zip(names, formats)))
# Structured dtype: just validate the names as needed
else:
ndtype.names = validate(names, nbfields=nbtypes,
ndtype.names = validate(names, nbfields=len(ndtype.names),
defaultfmt=defaultfmt)
# No implicit names
elif (nbtypes > 0):
elif ndtype.names is not None:
validate = NameValidator(**validationargs)
# Default initial names : should we change the format ?
if ((ndtype.names == tuple("f%i" % i for i in range(nbtypes))) and
if ((ndtype.names == tuple("f%i" % i for i in range(len(ndtype.names)))) and
(defaultfmt != "f%i")):
ndtype.names = validate([''] * nbtypes, defaultfmt=defaultfmt)
ndtype.names = validate([''] * len(ndtype.names), defaultfmt=defaultfmt)
# Explicit initial names : just validate
else:
ndtype.names = validate(ndtype.names, defaultfmt=defaultfmt)
Expand Down
2 changes: 1 addition & 1 deletion numpy/lib/npyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2147,7 +2147,7 @@ def encode_unicode_cols(row_tup):
outputmask = np.array(masks, dtype=mdtype)
else:
# Overwrite the initial dtype names if needed
if names and dtype.names:
if names and dtype.names is not None:
dtype.names = names
# Case 1. We have a structured type
if len(dtype_flat) > 1:
Expand Down
0