Vectorized calculation of new timeseries in pandas dataframe

I have a pandas dataframe and I am trying to estimate a new timeseries V(t) based on the values of an existing timeseries B(t). I have written a minimal reproducible example to generate a sample dataframe as follows: import pandas as pd import numpy as np lenb = 5000 lenv = 200 l = 5… Read More Vectorized calculation of new timeseries in pandas dataframe

Expand a dimension of 3-dimensional array into a diagonal matrix with vectorized computations

I have np.ndarray A of shape (N, M, D). I’d like to create np.ndarray B of shape (N, M, D, D) such that for every pair of fixed indices n, m along axes 0 and 1 B[n, m] = np.eye(A[n, m]) I understand how to solve this problem using cycles, yet I’d like to write… Read More Expand a dimension of 3-dimensional array into a diagonal matrix with vectorized computations

Vectorized way of checking a date column's calendar sequence

I have a dataframe which looks like this: Market Date Begin Date Settlement 0 2016-01-01 2016-01-01 26.1935 1 2016-01-01 2016-02-01 24.1071 2 2016-01-01 2016-03-01 21.0591 3 2016-01-01 2016-04-01 20.7348 4 2016-01-01 2016-05-01 20.2072 … … … … 265198 2022-09-21 2031-04-01 65.1300 265199 2022-09-21 2031-05-01 65.1300 265200 2022-09-21 2031-06-01 65.1300 265201 2022-09-21 2031-07-01 65.1300 265202 2022-09-21… Read More Vectorized way of checking a date column's calendar sequence

Vectorization assign the newest value based on datetime

I have two dataframe. The first dataframe have only one column: email, the first dataframe is a complete list of email. The second dataframe is a dataframe with three column: email, subscribe_or_unsubscribe, date. The second dataframe is a history of user subcribing or unsubscribing from the email system. The second dataframe is sorted by date… Read More Vectorization assign the newest value based on datetime

Vectorising a sum of scalar multiplied by a matrix, where the scaler is an element of a list

I’m trying to vectorise the following a = np.array([1,2]) b = np.array([[5,5],[5,5]]) target = 0 for _ in a: target = target + _ * b The above yields a 2×2 matrix where all entries are 15. How can I achieve this through vectorisation? I’ve been trying to cast a to be two 2×2 matrices,… Read More Vectorising a sum of scalar multiplied by a matrix, where the scaler is an element of a list

Optimizing results instead of apply; get df values and add to list of items

Simplifying my big problem into this I have the following datafarme: import pandas as pd df = pd.DataFrame({"letter":[‘A’,’B’,’D’,’E’,’G’,’W’,’G’,’M’,’E’,’Q’],’value’:[1,6,4,0,9,7,0,-1,5,3]}) and a list of items (name and value): items = [[‘John’,1],[‘Mike’,8],[‘Jessica’,4]] My goal is to add the letters in the df to the items such that if the value in the df + the value in the… Read More Optimizing results instead of apply; get df values and add to list of items

Vectorized str.replace for multiple characters in pandas

I have a dataframe: {‘country’: {0: ‘Afghanistan?*’, 1: ‘Albania?*’}, ‘region’: {0: ‘Asia’, 1: ‘Europe’}, ‘subregion’: {0: ‘Southern Asia’, 1: ‘Southern Europe’}, ‘rate_per_1000’: {0: 6.7, 1: 2.1}, ‘count’: {0: ‘2,474’, 1: ’61’}, ‘year’: {0: 2018, 1: 2020}, ‘source’: {0: ‘NSO’, 1: ‘NSO’}} country region subregion rate_per_1000 count year source 0 Afghanistan?* Asia Southern Asia 6.7 2,474… Read More Vectorized str.replace for multiple characters in pandas