Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

ENH: Fix repr of np.record objects to match np.void types #10412#10424

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall commits
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
8 changes: 6 additions & 2 deletionsnumpy/core/records.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -223,10 +223,14 @@ class record(nt.void):
__module__ = 'numpy'

def __repr__(self):
return self.__str__()
if get_printoptions()['legacy'] == '1.13':
return self.__str__()
return super(record, self).__repr__()

def __str__(self):
return str(self.item())
if get_printoptions()['legacy'] == '1.13':
return str(self.item())
return super(record, self).__str__()

def __getattribute__(self, attr):
if attr in ['setfield', 'getfield', 'dtype']:
Expand Down
40 changes: 28 additions & 12 deletionsnumpy/core/tests/test_records.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,6 +4,7 @@
import collections
import pickle
import warnings
import textwrap
from os import path

import numpy as np
Expand DownExpand Up@@ -103,7 +104,7 @@ def test_recarray_from_obj(self):

def test_recarray_repr(self):
a = np.array([(1, 0.1), (2, 0.2)],
dtype=[('foo',int), ('bar',float)])
dtype=[('foo','<i4'), ('bar','<f8')])
a = np.rec.array(a)
assert_equal(
repr(a),
Expand All@@ -112,6 +113,32 @@ def test_recarray_repr(self):
dtype=[('foo', '<i4'), ('bar', '<f8')])""")
)

# make sure non-structured dtypes also show up as rec.array
a = np.array(np.ones(4, dtype='f8'))
assert_(repr(np.rec.array(a)).startswith('rec.array'))

# check that the 'np.record' part of the dtype isn't shown
a = np.rec.array(np.ones(3, dtype='i4,i4'))
assert_equal(repr(a).find('numpy.record'), -1)
a = np.rec.array(np.ones(3, dtype='i4'))
assert_(repr(a).find('dtype=int32') != -1)

def test_0d_recarray_repr(self):
# testing infered integer types is unpleasant due to sizeof(int) varying
arr_0d = np.rec.array((np.int32(1), 2.0, np.datetime64('2003')))
assert_equal(repr(arr_0d), textwrap.dedent("""\
rec.array((1, 2., '2003'),
dtype=[('f0', '<i4'), ('f1', '<f8'), ('f2', '<M8[Y]')])"""))

record = arr_0d[()]
assert_equal(repr(record), "(1, 2., '2003')")
# 1.13 converted to python scalars before the repr
try:
np.set_printoptions(legacy='1.13')
assert_equal(repr(record), '(1, 2.0, datetime.date(2003, 1, 1))')
finally:
np.set_printoptions(legacy=False)

def test_recarray_from_repr(self):
a = np.array([(1,'ABC'), (2, "DEF")],
dtype=[('foo', int), ('bar', 'S4')])
Expand DownExpand Up@@ -197,17 +224,6 @@ class C(np.recarray):
assert_equal(arr2.dtype.type, arr.dtype.type)
assert_equal(type(arr2), type(arr))

def test_recarray_repr(self):
# make sure non-structured dtypes also show up as rec.array
a = np.array(np.ones(4, dtype='f8'))
assert_(repr(np.rec.array(a)).startswith('rec.array'))

# check that the 'np.record' part of the dtype isn't shown
a = np.rec.array(np.ones(3, dtype='i4,i4'))
assert_equal(repr(a).find('numpy.record'), -1)
a = np.rec.array(np.ones(3, dtype='i4'))
assert_(repr(a).find('dtype=int32') != -1)

def test_recarray_from_names(self):
ra = np.rec.array([
(1, 'abc', 3.7000002861022949, 0),
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp