I am using pandas/python and I have two date time series s1 and s2, that have been generated using the ‘to_datetime’ function on a field of the df containing dates/times. When I subtract s1 from s2 s3 = s2 – s1 I get a series, s3, of type timedelta64[ns] How do I look at 1 element of the ser…
Tag: numpy
unexpected result in numpy array slicing (view vs copy)
I’m trying to reduce the amount of copying in my code and I came across surprising behavior when dealing with numpy array slicing and views, as explained in: Scipy wiki page on copying numpy arrays I’ve stumbled across the following behavior, which is unexpected for me: Case 1.: As expected, this …
in Numpy, how to zip two 2-D arrays?
For example I have 2 arrays How can I zip a and b so I get ? Answer You can use dstack: If you must have tuples: For Python 3+ you need to expand the zip iterator object. Please note that this is horribly inefficient:
What’s the fastest way in Python to calculate cosine similarity given sparse matrix data?
Given a sparse matrix listing, what’s the best way to calculate the cosine similarity between each of the columns (or rows) in the matrix? I would rather not iterate n-choose-two times. Say the input matrix is: The sparse representation is: In Python, it’s straightforward to work with the matrix-i…
Dropping infinite values from dataframes in pandas?
How do I drop nan, inf, and -inf values from a DataFrame without resetting mode.use_inf_as_null? Can I tell dropna to include inf in its definition of missing values so that the following works? Answer First replace() infs with NaN: and then drop NaNs via dropna(): For example: The same method also works for …
How can I map True/False to 1/0 in a Pandas DataFrame?
I have a column in python pandas DataFrame that has boolean True/False values, but for further calculations I need 1/0 representation. Is there a quick pandas/numpy way to do that? Answer A succinct way to convert a single column of boolean values to a column of integers 1 or 0:
student t confidence interval in python
I am interested in using python to compute a confidence interval from a student t. I am using the StudentTCI() function in Mathematica and now need to code the same function in python http://reference.wolfram.com/mathematica/HypothesisTesting/ref/StudentTCI.html I am not quite sure how to build this function …
how is axis indexed in numpy’s array?
From Numpy’s tutorial, axis can be indexed with integers, like 0 is for column, 1 is for row, but I don’t grasp why they are indexed this way? And How do I figure out each axis’ index when coping with multidimensional array? Answer By definition, the axis number of the dimension is the index…
Puzzled on the ndim from Numpy
Above is my code. the output is: I have checked the page from [here] (Link) I expected a.dim = 2 or 3, but it is 4. Why? Could you give me some hints? Thanks Answer The tuple you give to zeros and other similar functions gives the ‘shape’ of the array (and is available for any array as a.shape). T…
Using arctan / arctan2 to plot a from 0 to 2π
I am trying to replicate a plot in Orbital Mechanics by Curtis, but I just can’t quite get it. However, I have made head way by switching to np.arctan2 from np.arctan. Maybe I am implementing arctan2 incorrectly? In the image below, there are discontinuities popping up. The function is supposed to be sm…