Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
FIX: compute str column width
The division was always returning 1, use length of first element
instead.
  • Loading branch information
tacaswell committed May 16, 2016
commit 1900372399315a345bd5cb32380af3bc00677886
4 changes: 1 addition & 3 deletions lib/matplotlib/mlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -3188,9 +3188,7 @@ def get_justify(colname, column, precision):
ntype = column.dtype

if np.issubdtype(ntype, str) or np.issubdtype(ntype, bytes):
# The division below handles unicode stored in array, which could
# have 4 bytes per char
length = max(len(colname), column.itemsize // column[0].itemsize)
length = max(len(colname), len(column[0]))
Copy link
Contributor

@dopplershift dopplershift May 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced len() will do what we want if the data for a column is ['foo', 'bars']. Probably should change the test to have some differing string sizes and see what happens.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is that allowed in recarrays? The types look like fixed-width string, not variable width.

Copy link
Contributor

@dopplershift dopplershift May 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In [18]: a = np.array([(1, 'foo'), (2, 'bars')], dtype=np.dtype([('i', np.int32), ('s', np.str, 4)]))

In [19]: a
Out[19]:
array([(1, 'foo'), (2, 'bars')],
      dtype=[('i', '<i4'), ('s', '<U4')])

In [20]: len(a['s'][0])
Out[20]: 3

I couldn't find a numpy function to get that 4 from the <U4, which is what we really want.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it turns out my original code was broken as well:

In [29]: a['s'][0].itemsize
Out[29]: 3

WTF?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you want a.dtype['s'].itemsize == 16 ( / 4 for Unicode == 4)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

len() is not the right thing. The best I can come up with is int(a.dtype['s'].str[2:])

return 0, length+padding, "%s" # left justify

if np.issubdtype(ntype, np.int):
Expand Down
6 changes: 3 additions & 3 deletions lib/matplotlib/tests/test_mlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,9 @@ def test_csv2txt_basic(self):
(str('y'), np.int8),
(str('s'), str, 3),
(str('s2'), str, 4)]))
truth = (' x y s s2\n'
' 1.000 2 foo bing \n'
' 2.000 3 bar blah ').splitlines()
truth = (' x y s s2\n'
' 1.000 2 foo bing \n'
' 2.000 3 bar blah ').splitlines()
assert_equal(mlab.rec2txt(a).splitlines(), truth)


Expand Down