How to Convert Np.Int64 into Python Int64 for Pandasseries

How to convert np.int64 into python int64 for PandasSeries?

The problem was in wrong indexation:

  • first index was from 83 to 1161 and after 1161, where should've been 1161, was 83 again and next values were 83 + 1 etc.

Thus, problem was solved by .reset_index()

df_with_new_one.reset_index(drop = True, inplace = True)

Thanks you all for answers!

Convert a numpy int64 into a list

One possible solution is list comprehenstion:

L = [(k, v) for k, v in series.items()]

Or convert values to DataFrame anf then to list ot tuples:

L = list(map(tuple, series.reset_index().values.tolist()))

Or to MultiIndex:

L = series.to_frame('a').set_index('a', append=True).index.tolist()

print (L)
[('Zed', 49), ('Kassadin', 39), ('Cassiopeia', 34), ('RekSai', 33), ('Nidalee', 30)]

Is this a bug? Cannot simply change dict keys from numpy to primitive data types

No, this is not a bug.

This code shows that the key has not changed during the update:

import numpy as np
d = {np.int64(0): None}

for k, v in d.items():
print(str(type(k))) # <class 'numpy.int64'>
k_nat = k.item()
print(str(type(k_nat))) # <class 'int'>
print(d) # {0: None}
d.update({k_nat:1})
print(d) # {0: 1}
# Therefore update using int was successful
# But key does not change
print(type(list(d.keys())[0])) # → <class 'numpy.int64'>

for k, v in d.items():
print(str(type(k))) # <class 'numpy.int64'>

Python treats int(0) and np.int64(0) w.r.t. dict-access. But the original key is not changed (only the value). Note that both int(0) and np.int64(0) are represented as 0 in expressions like print(d). So they look like if they are the same. However, they are equal but not identical.

in particular we have this behavior

print(d[np.int64(0)] == d[int(0)]) # True
print(np.int64(0) == int(0)) # True
print(np.int64(0) is int(0)) # False

If you want to convert the key-type, you can use:

new_d = {int(k): v for k, v in d.items()}
print(type(list(new_d.keys())[0])) # <class 'int'>

For some classes it is indeed possible to change the type of an object without changing the id of the object and thus it still works as the same dict-key:

class A(object):
pass

class B(object):
pass

d = {A(): None}

print(type(list(d.keys())[0])) # <class '__main__.A'>

# change type of object but not the object itself
list(d.keys())[0].__class__ = B
print(type(list(d.keys())[0])) # <class '__main__.B'>

However, for some other classes (including np.int64) this is not possible:

x = np.int64(0)
try:
x.__class__ = int
except TypeError as err:
print(err) # __class__ assignment only supported for heap types or ModuleType subclasses

Python: Trying to convert single column from Float to Int

I'm confused. Based on your given input, your code works for me:

import pandas as pd, numpy as np
from io import StringIO

input = """
movieId,title,Year
1,Toy Story (1995),1995.0
2,Jumanji (1995),1995.0
"""

df = pd.read_csv(StringIO(input))
df['Year'] = df['Year'].dropna().apply(np.int64)
print(df["Year"].head())

Output

0    1995
1 1995
Name: Year, dtype: int64

Edit: Following the discussion below.

import pandas as pd, numpy as np
from io import StringIO

input = """
movieId,title,genres
1,Toy Story (1995),Adventure|Animation|Children|Comedy|Fantasy
2,Jumanji (1995),Adventure|Children|Fantasy
3,Grumpier Old Men (1995),Comedy|Romance
4,Waiting to Exhale (1995),Comedy|Drama|Romance
5,Father of the Bride Part II (1995),Comedy
6,Heat (1995),Action|Crime|Thriller
7,Sabrina (1995),Comedy|Romance
8,Tom and Huck (1995),Adventure|Children
9,Sudden Death (1995),Action
10,GoldenEye (1995),Action|Adventure|Thriller
11,"American President, The (1995)",Comedy|Drama|Romance
12,Dracula: Dead and Loving It (1995),Comedy|Horror
13,Balto (1995),Adventure|Animation|Children
14,Nixon (1995),Drama
"""

df = pd.read_csv(StringIO(input))
df["Year"] = df["title"].apply(lambda title: title[-5:-1])
df['Year'] = df['Year'].dropna().apply(np.int64)
print(df["Year"].head())

Output

0    1995
1 1995
2 1995
3 1995
4 1995
...
Name: Year, dtype: int64


Related Topics



Leave a reply



Submit