Skip to content
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

[FEA] How to read hex as int using cudf #1925

Closed
VibhuJawa opened this issue Jun 4, 2019 · 10 comments · Fixed by #2149
Closed

[FEA] How to read hex as int using cudf #1925

VibhuJawa opened this issue Jun 4, 2019 · 10 comments · Fixed by #2149
Assignees
Labels
cuIO cuIO issue feature request New feature or request

Comments

@VibhuJawa
Copy link
Member

What is your question?
What is the cudf equivalent way to read hex as int columns using cudf.

Example of doing this in pandas.

import cudf
import pandas as pd

# create hex csv
fname = 'test.csv'
df = pd.DataFrame()
df['hex_col'] = ['9512c20b']*10
df.to_csv(fname,index=False)

## read_csv using pandas
from functools import partial
df = pd.read_csv(fname, converters={'hex_col': partial(int, base=16)})
df.head(5)

Output

	hex_col
0	2501034507
1	2501034507
2	2501034507
3	2501034507
4	2501034507
@VibhuJawa VibhuJawa added Needs Triage Need team to review and classify question Further information is requested labels Jun 4, 2019
@mjsamoht mjsamoht added cuIO cuIO issue feature request New feature or request and removed Needs Triage Need team to review and classify question Further information is requested labels Jun 5, 2019
@mjsamoht
Copy link
Contributor

If the numbers start with "0x" they will be interpreted as hexadecimal numbers. This was added in #973.

@mjsamoht mjsamoht added question Further information is requested and removed feature request New feature or request labels Jun 21, 2019
@VibhuJawa
Copy link
Member Author

We have datasets where hex-numbers do not start with 0x . We want this feature for such numbers.

For eg: We have numbers like 9512c20b which are not appended with 0x . There was discussion about this in the rapidsdev-io channel.

@mjsamoht
Copy link
Contributor

I see. That is currently not supported. One option could be to add a hex datatype that can be specified in dtype. Or we could perhaps automatically treat [0-9a-f] as "digits".

@VibhuJawa
Copy link
Member Author

VibhuJawa commented Jun 21, 2019

I see. That is currently not supported. One option could be to add a hex datatype that can be specified in dtype. Or we could perhaps automatically treat [0-9a-f] as "digits".

FWIW, I like the first option of adding a hex datatype.

Automatically treating [0-9a-f] as "digits" makes it inconsistent with pandas behavior .

@mjsamoht mjsamoht added feature request New feature or request and removed question Further information is requested labels Jun 25, 2019
@mjsamoht mjsamoht changed the title [QST] How to read hex as int using cudf [FEA] How to read hex as int using cudf Jun 25, 2019
@j-ieong
Copy link
Contributor

j-ieong commented Jun 28, 2019

@VibhuJawa For adding a hex dtype, do you expect it to be 32-bit or 64-bit for memory allocation? Or would you prefer more explicit hex32 and hex64?

If you have 9512c20b, then a simple print on the Python side would yield -1793932789 instead of 2501034507 that you may expect/get from PANDAS, as GDF_INT32 is a signed int underneath.

@VibhuJawa
Copy link
Member Author

@VibhuJawa For adding a hex dtype, do you expect it to be 32-bit or 64-bit for memory allocation? Or would you prefer more explicit hex32 and hex64?

If you have 9512c20b, then a simple print on the Python side would yield -1793932789 instead of 2501034507 that you may expect/get from PANDAS, as GDF_INT32 is a signed int underneath.

I would prefer a more explicit hex32 and hex64 dtype.

I also do not sure know how to get , -1793932789 with print.

x = int("9512c20b", 16)
print(x)

Output

2501034507

@randerzander Thoughts ?

@j-ieong
Copy link
Contributor

j-ieong commented Jun 28, 2019

I would prefer a more explicit hex32 and hex64 dtype.

I also do not sure know how to get , -1793932789 with print.

x = int("9512c20b", 16)
print(x)

Output

2501034507

I believe int in Python 3 is larger than 32-bits. The output cudf dataframe would be returned as either GDF_INT32 or GDF_INT64 dtype; the PANDAS equivalent dataframe would be:

>>> import pandas as pd
>>> df = pd.DataFrame([int('9512c20b', 16)], dtype='int32')
>>> print(df)
            0
0 -1793932789
>>> df = pd.DataFrame([int('9512c20b', 16)], dtype='int64')
>>> print(df)
            0
0  2501034507

@VibhuJawa
Copy link
Member Author

I would prefer a more explicit hex32 and hex64 dtype.
I also do not sure know how to get , -1793932789 with print.

x = int("9512c20b", 16)
print(x)

Output

2501034507

I believe int in Python 3 is larger than 32-bits. The output cudf dataframe would be returned as either GDF_INT32 or GDF_INT64 dtype; the PANDAS equivalent dataframe would be:

>>> import pandas as pd
>>> df = pd.DataFrame([int('9512c20b', 16)], dtype='int32')
>>> print(df)
            0
0 -1793932789
>>> df = pd.DataFrame([int('9512c20b', 16)], dtype='int64')
>>> print(df)
            0
0  2501034507

Aah, got it. I think with int32 we may have overflows (as in this case) . A lot of times the hex inputs are hashes created to anonymize the data.

We may even want to look at supporting bigger hex ints later.

import numpy as np

num = int('9512c20b', 16)
i32 = np.iinfo(np.int32)
i64 = np.iinfo(np.int64)
print(i32.max<num)
print(i64.max<num)
print(np.int32(num))
print(np.int64(num))
True
False
-1793932789
2501034507

@j-ieong
Copy link
Contributor

j-ieong commented Jun 29, 2019

So I decided on adding hex32, hex64, and simply hex. The latter aliases hex64 as its more user-friendly to default to the larger size, and hexadecimal data would probably require it.
If it's known that the dataset will not overflow, then hex32 is available to save some memory.

Let me know what you think...

@VibhuJawa
Copy link
Member Author

So I decided on adding hex32, hex64, and simply hex. The latter aliases hex64 as its more user-friendly to default to the larger size, and hexadecimal data would probably require it.
If it's known that the dataset will not overflow, then hex32 is available to save some memory.

Let me know what you think...

I agree with this approach . I think alias will make it intuitive for the user . Thanks a lot for your work on this.

rapids-bot bot pushed a commit that referenced this issue Mar 1, 2022
This PR silences warnings in `test_csv.py`. (I am working through one test file at a time so we can enable `-Werr` in the future.)

The only warning in this file is related to integer overflow in pandas. Currently, the test data is as follows:
https://github.com/rapidsai/cudf/blob/21325e8348f33b28e434d08d687a28f251c38f67/python/cudf/cudf/tests/test_csv.py#L1313-L1319

First, I note that this "hex" dtype is not part of the pandas API. It is a cuDF addition (#1925, #2149).

Note that there are dtypes for `int32` / `hex32`, and the test data contains both a negative value `-0x1000` and a value `9512c20b`. The negative value `-0x1000` has a sensible interpretation if the results are meant to be signed, but then the value `9512c20b` is out of range (the maximum signed 32-bit value would be `0x7FFFFFFF` and the minimum signed 32-bit value would be `0x80000000`, using the big-endian convention of the parser). Recognizing this, pandas throws a `FutureWarning` when parsing the data `9512c20b` as `int32`, and unsafely wraps it to a negative value. This behavior will eventually be replaced by an `OverflowError`.

In the future, we may need to decide if cuDF should raise an `OverflowError` when exceeding `0x7FFFFFFF` for consistency with pandas, or decide to use unsigned integers when parsing "hex" dtypes and compare to pandas' unsigned types in this test.

Authors:
  - Bradley Dice (https://github.com/bdice)

Approvers:
  - Vukasin Milovanovic (https://github.com/vuule)
  - https://github.com/brandon-b-miller

URL: #10362
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cuIO cuIO issue feature request New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants