Python: numpy.insert NaN value

2024/9/20 12:14:21

I'm trying to insert NaN values to specific indices of a numpy array. I keep getting this error:

TypeError: Cannot cast array data from dtype('float64') to dtype('int64') according to the rule 'safe'

When trying to do so with the following code.

x = np.array(range(1,11))
x = np.insert(x, 5, np.nan, axis=0)

However, I can append NaN values to the end of the array with no problem.

x = np.array(range(1,11))
x = np.append(x, np.nan)

Why is this and how can I insert NaN values in my array?

Answer

With x=np.array(range(1,11)), the dtype by default is int64, which prevents you to insert a float.

The easiest is to force the dtype to float directly:

x = np.array(range(1, 11), dtype=float)

With np.insert, you're limited to the dtype of the initial array (the temporary arrays created below the hood use the dtype of the input).

With np.append, however, you're actually using np.concatenate, which creates an array with the "largest" dtype of its inputs: in your example, x is then cast to float.


Note that you could simply use the np.arange function:

x = np.arange(1, 11, dtype=float)
https://en.xdnf.cn/q/72507.html

Related Q&A

Identify external workbook links using openpyxl

I am trying to identify all cells that contain external workbook references, using openpyxl in Python 3.4. But I am failing. My first try consisted of:def find_external_value(cell): # identifies an e…

3D-Stacked 2D histograms

I have a bunch of 2D histograms (square 2D numpy arrays) that I want to stack in 3D like so:(Image from: Cardenas, Alfredo E., et al. "Unassisted transport of N-acetyl-L-tryptophanamide through me…

Python and mySQLdb error: OperationalError: (1054, Unknown column in where clause)

Hey all, Im getting an error OperationalError: (1054, "Unknown column XX in where clause")Where XX is the value of CLASS in the following codeconn = MySQLdb.connect(host = "localhost&quo…

Best Python GIS library? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, argum…

Build a class with an attribute in one line

How do I write a one-liner for the following? class MyClass(): content = {} obj = MyClass()

Python Imports, Paths, Directories Modules

Let me start by saying Ive done extensive research over the course of the past week and have not yet found actual answers to these questions - just some fuzzy answers that dont really explain what is g…

Finding location in code for numpy RuntimeWarning

I am getting warnings like these when running numpy on reasonably large pipeline. RuntimeWarning: invalid value encountered in true_divideRuntimeWarning: invalid value encountered in greaterHow do I fi…

Django, Angular, DRF: Authentication to Django backend vs. API

Im building an app with a Django backend, Angular frontend, and a REST API using Django REST Framework for Angular to consume. When I was still working out backend stuff with a vanilla frontend, I used…

Django view testing

Im trying to figure out if there is a quick way to test my django view functions form either the python or django shell. How would I go about instantiating and passing in faux HTTPrequest object?

Remove non-ASCII characters from string columns in pandas

I have panda dataframe with multiple columns which mixed with values and unwanted characters. columnA columnB columnC ColumnD \x00A\X00B NULL \x00C\x00D 123 \x00E\X00F…