How to append a selection of a numpy array to an empty numpy array

2024/11/15 3:34:07

I have a three .txt files to which I have successfully made into a numpy array. If you are curious these files are Level 2 data from the Advanced Composition Experiment (ACE). The particular files are found in the MAG and SWEPAM sections and are 16 second average and 64 second average, respectively. The data in a nut shell is representative of the z-component magnetic field of an inbound particle field, its constituents by measure of counts per area, and its velocity. Currently the focus of the study is on inbound hydrogen, but I digress. The code is as follows I use to read and save the files (as well as fix any errors) is provided below:

Bz = np.loadtxt(r"/home/ary/Desktop/Arya/Project/Data/AC/MAG/ACE_MAG_Data_SEPT_18_2015.txt", dtype = bytes).astype(float)
SWEPAM_HV = np.loadtxt(r"/home/ary/Desktop/Arya/Project/Data/ACE/SWEPAM/Proton_Density/ACE_SWEPAM_H_Density_20150918.txt", dtype = bytes).astype(float)
SWEPAM_HD = np.loadtxt(r"/home/ary/Desktop/Arya/Project/Data/ACE/SWEPAM/Proton_Speed/ACE_SWEPAM_H_Velocity_20150918.txt",dtype = bytes).astype(float)Bz = np.ma.masked_array(Bz, Bz <= -999, fill_value = 0)
SWEPAM_HD = np.ma.masked_array(SWEPAM_HD, SWEPAM_HD <= -999, fill_value = 0)
SWEPAM_HV = np.ma.masked_array(SWEPAM_HV, SWEPAM_HV <= -999, fill_value = 0)Mag_time = np.arange(0,86400, 16, dtype = float)
SWEPAM_time = np.arange(0,86400,64, dtype = float)

However, within these array I am particularly interested in only the 1349th position to the 2024th position. These numbers are of interest because of my investigation into an anomaly which happened between these two points. So I figured the following would lead me to success. To which it hasn't and many variations have failed too. I present to you the most recent script I have right now:

Mag_time_prime = np.array([])
Bz_prime = np.array([])
for i in range(1349,2024):append(Mag_time_prime,Mag_time[i]).astype(float)append(Bz_prime,Bz[i]).astype(float)
print(Mag_time_prime.shape)
print(Bz_prime.shape)

I had figured that by making empty arrays (I did try np.empty(0) for the primes and couldn't get that to work for me) that I could just make a for loop to locate and append the i_th position from the Bz and Mag_time to the empty 'prime' arrays within the specified range. However the 'prime' arrays have continuously popped out empty arrays. So my question, where have I gone wrong and how should I fix it?

Answer

List append acts on the list itself:

In [1]: alist = []
In [2]: alist.append(5)
In [3]: alist.append(3)
In [4]: alist
Out[4]: [5, 3]

np.append does not change its arguments:

In [5]: arr = np.array([])
In [6]: np.append(arr,1)
Out[6]: array([ 1.])
In [7]: np.append(arr,2)
Out[7]: array([ 2.])
In [8]: arr
Out[8]: array([], dtype=float64)

You have to assign the value of append back to arr to get the list equivalent behavior:

In [9]: arr=np.append(arr,1)
In [10]: arr=np.append(arr,2)
In [11]: arr
Out[11]: array([ 1.,  2.])

Each time you use np.append you create a new copy (it uses np.concatenate). For one or two times that's ok, but if done repeatedly it is inefficient.

The preferred way is to use list append to build a list, and then make an array from that:

In [12]: np.array(alist)
Out[12]: array([5, 3])

You have to understand np.concatenate before you can use np.append properly. It is a poor substitute for list append.

https://en.xdnf.cn/q/119088.html

Related Q&A

Error saving and loading a list of matrices

I have a list "data_list", and I would save it in order to load it in another script. First of all I converted it in an array, in this way:data_array = np.array(data_list)Then I saved it:np.s…

Trying to interact with HTML page elements, but none of them are found

Im trying to scrape a webpage using Selenium, but when I try to pass the XPath of a button, I get an error saying that this element does not exist. I tried with another website, and it worked perfectly…

Duplicating an XML element and adding it to a specific position in XML file using python

I have a xml file in which content looks like this: xml_content_to_search = <Document ProviderID="TD" DecimalMarker="comma" Website="https://erc-viewer.sap.com/"> &l…

How do I fix this Gets server error, which is causing display issues?

The list in the left column of ontariocourts311.ca, along with the body of the page under the image intermittently fail to display (which is fixed by refreshing the page). Im a Noob, and have tried var…

Installing Scipy for Windows

I am trying to install Scipy on my computer. I did it by using the command pip install Scipy. (pip & numpy are up-to-date and I am using Python 3.6) I also tried it with Pycharm, but it didnt worke…

Python Opencv, dont put circle on the video

I wrote the following script with OpenCVimport cv2 import numpy as npcap = cv2.VideoCapture(0) ix, iy = -1, -1def draw_circle(event, x, y, flags, param):global ixglobal iyix,iy = x,yif event == cv2.EVE…

List coordinates between a set of coordinates

This should be fairly easy, but Im getting a headache from trying to figure it out. I want to list all the coordinates between two points. Like so:1: (1,1) 2: (1,3) In between: (1,2)Or1: (1,1) 2: (5,1)…

NA values in column is not NaN Pandas Python [duplicate]

This question already has answers here:Prevent pandas from interpreting NA as NaN in a string(7 answers)Closed 2 years ago.I got a CSV File. I got a column Product. One of the products in it, called NA…

How to fix pandas column data

Workflow is :Read CSV file using Pythons pandas library and get Variation Column Variation Column data isVariation ---------- Color Family : Black, Size:Int:L Color Family : Blue, Size:Int:M Color Fam…

Connect to Oracle Database and export data as CSV using Python

I want to connect oracle database to python and using select statement whatever result i will get, I want that result to be exported as csv file in sftp location. I know we can connect oracle with pyth…