0

I wish to have an int matrix which has only its first column filled and the rest of elements are Null. Sorry but, I have a background of R. So, I know if I leave some Null elements it would be easier to manage them later. Meanwhile, if I leave 0 then it would be lots of problems later.

I have the following code:

   import numpy as np
   import numpy.random as random
   import pandas as pa

    def getRowData():
        rowDt = np.full((80,20), np.nan)
        rowDt[:,0] =  random.choice([1,2,3],80) # Set the first column
        return  rowDt

I wish that this function returns the int, but seems that it gives me float.

I have seen this link, and tried the below code:

return  pa.to_numeric(rowDt)

But, it did not help me. Also the rowDT object does not have .astype(<type>).

How can I convert an int array?

5
  • 1
    I think your code is wrong, shouldnt it be np.random.choice the choice function in the random module does not accept a second argument. Also, if rowDT has np.nan in it then it cannot be a integer array as np.nan cannot be represetned by integers. Would that explain it? Commented Mar 27, 2019 at 19:49
  • You could use np.nan_to_num but then you'd either use a different value for full in the first place, or you presumably need nan in which case the array cannot be of type int Commented Mar 27, 2019 at 19:51
  • numpy object do have .astype(<type>) function (version 1.14.3) and I was able to convert it to int, which version are you using? Commented Mar 27, 2019 at 19:51
  • @YilunZhang i am using 1.15.4 Commented Mar 27, 2019 at 19:54
  • A limitation of pandas: you cannot have int dtype with null, i.e. nan Commented Mar 27, 2019 at 23:46

2 Answers 2

1

You create a full (np.full ) matrix of np.nan, which holds float dtype. This means you start off with a matrix defined to hold float numbers, not integers.

To fix this, fefine a full matrix with the integer 0 as initial value. That way, the dtype of your array is np.int and there is no need for astype or type casting.

rowDt = np.full((80,20), 0)

If you still wish to hold np.nan in your matrix, then I'm afraid you cannot use numpy arrays for that. You either hold all integers, or all floats.

Sign up to request clarification or add additional context in comments.

4 Comments

i wish to have an int matrix wich has only its first column filled and the rest of elements are Null . Sorry, i have a background of R. So, i know if i make i have some Null elements it would be eaiser to manage them later. Meanwhile if i feel them with 0 then it would be lots of problem later.
@KamyarParastesh you can't have null and int in the same numpy array.
I mean, you can, but is highly not recommended. np.array([np.nan, 1], dtype=object) works, but you lose all the properties of a np array, and the use of numpy here gets weird.
Python doesn't have null. nan is a float. None is a unique object, not a number.
0

You can use numpy.ma.masked_array() to create a numpy masked array

The numpy masked array "remembers" which elements are "masked". It provides methods and functions similar to those of numpy arrays, but excluding the masked values from the computations (such as, eg, mean()).

Once you have the masked array, you can always mask or unmask specific elements or rows or columns of elements whenever you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.