Navigation Menu
-
-
Notifications
You must be signed in to change notification settings - Fork 12.5k
ENH: usecols now accepts an int when only one column has to be read #6656
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
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 |
|---|---|---|
|
|
@@ -608,6 +608,29 @@ def test_usecols(self): | |
| x = np.loadtxt(c, dtype=float, usecols=np.array([1, 2])) | ||
| assert_array_equal(x, a[:, 1:]) | ||
|
|
||
| # Testing with an integer instead of a sequence | ||
|
Member
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 you want to test with something really crazy, could test with: but I am OK with leaving it out.
Contributor
Author
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. You're right it's better to add a test with a custom integer type. If I had done that from the beginning I would have seen the problem with my previous way to check integers. |
||
| for int_type in [int, np.int8, np.int16, | ||
| np.int32, np.int64, np.uint8, np.uint16, | ||
| np.uint32, np.uint64]: | ||
| to_read = int_type(1) | ||
| c.seek(0) | ||
| x = np.loadtxt(c, dtype=float, usecols=to_read) | ||
| assert_array_equal(x, a[:, 1]) | ||
|
|
||
| # Testing with some crazy custom integer type | ||
| class CrazyInt(object): | ||
| def __index__(self): | ||
| return 1 | ||
|
|
||
| crazy_int = CrazyInt() | ||
| c.seek(0) | ||
| x = np.loadtxt(c, dtype=float, usecols=crazy_int) | ||
| assert_array_equal(x, a[:, 1]) | ||
|
|
||
| c.seek(0) | ||
| x = np.loadtxt(c, dtype=float, usecols=(crazy_int,)) | ||
| assert_array_equal(x, a[:, 1]) | ||
|
|
||
| # Checking with dtypes defined converters. | ||
| data = '''JOE 70.1 25.3 | ||
| BOB 60.5 27.9 | ||
|
|
@@ -619,6 +642,21 @@ def test_usecols(self): | |
| assert_equal(arr['stid'], [b"JOE", b"BOB"]) | ||
| assert_equal(arr['temp'], [25.3, 27.9]) | ||
|
|
||
| # Testing non-ints in usecols | ||
| c.seek(0) | ||
| bogus_idx = 1.5 | ||
| assert_raises_regex( | ||
| TypeError, | ||
| '^usecols must be.*%s' % type(bogus_idx), | ||
| np.loadtxt, c, usecols=bogus_idx | ||
| ) | ||
|
|
||
| assert_raises_regex( | ||
| TypeError, | ||
| '^usecols must be.*%s' % type(bogus_idx), | ||
| np.loadtxt, c, usecols=[0, bogus_idx, 0] | ||
| ) | ||
|
|
||
| def test_fancy_dtype(self): | ||
| c = TextIO() | ||
| c.write('1,2,3.0\n4,5,6.0\n') | ||
|
|
||

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.
Actually, I think there is no reason to create usecols_as_list at all. Also the comment is rather unnecessary "existing code" is not existing code anymore as soon as we put this in ;).
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.
By existing code I meant all the lines below this one where usecols is used.
You're right I can use usecols and avoid the creation of usecols_as_list. I wished to make this easier to read, I hate code like foo=some_fancy_type(foo) buried deep in some code and then waste time trying to understand why foo is not any more what it was at the beginning.