0

I would like to know if I can use native types for number in a pandas DataFrame instead of numpy types.

I try to convert from numpy to native int with this code:

# Convert numpy to native type
df['a'] = df['a'].astype(int)
for index, row in df.iterrows():
    # If this is a numpy type then it has an item method
    if hasattr(df['a'][index], 'item'):
        df['a'][index] = df['a'][index].item()

But when I check the type, it is always a numpy.int64

cyprieng
  • 706
  • 6
  • 16
  • Hope this will help [NUMPY TYPES TO NATIVE TYPES](http://stackoverflow.com/questions/9452775/converting-numpy-dtypes-to-native-python-types) – Tanmaya Meher Jul 21 '15 at 10:55
  • Why are you trying to do this? – hpaulj Jul 21 '15 at 10:59
  • @hpaulj i want my dataframe to be serializable, but numpy.int are not. – cyprieng Jul 21 '15 at 14:25
  • A `numpy` array stores its data in a contiguous buffer. If dtype is int, those bytes would be the same a Python ints. It's the public interface that gives `df['a'][1]` that `np.int64` wrapper. It is still a numpy object, regardless of `dtype`. It's the `.item()` method that takes it out of the numpy wrapper. How are 'serializing' this data? – hpaulj Jul 21 '15 at 16:02
  • Look at `np.lib.npyio.format` or `numpy/lib/format.py` to see how `np.save` writes an array to a file. – hpaulj Jul 21 '15 at 16:08
  • In addition, don't confuse `type` (a Python concept) and `dtype` (a numpy attribute). – hpaulj Jul 21 '15 at 16:09
  • *"i want my dataframe to be serializable,"* Don't put this info in the comments; [edit] your question instead. it makes it different from the question mentioned by @TanmayaMeher. – jfs Jul 21 '15 at 21:02
  • http://stackoverflow.com/questions/11561932/ - SO question on why JSON `dumps` works on `a.tolist()` but not on `list(a)`. One produces a list of `int`, the other a list of `np.int`. – hpaulj Jul 21 '15 at 22:35

0 Answers0