Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9183667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:51:41+00:00 2026-06-17T18:51:41+00:00

I have two Pandas TimeSeries: x , and y , which I would like

  • 0

I have two Pandas TimeSeries: x, and y, which I would like to sync “as of”. I would like to find for every element in x the latest (by index) element in y that preceeds it (by index value). For example, I would like to compute this new_x:

x       new_x
----    -----
13:01   13:00  
14:02   14:00

y
----
13:00
13:01
13:30
14:00

I am looking for a vectorized solution, not a Python loop. The time values are based on Numpy datetime64. The y array’s length is in the order of millions, so O(n^2) solutions are probably not practical.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:51:42+00:00Added an answer on June 17, 2026 at 6:51 pm

    In some circles this operation is known as the “asof” join. Here is an implementation:

    def diffCols(df1, df2):
        """ Find columns in df1 not present in df2
        Return df1.columns  - df2.columns maintaining the order which the resulting
        columns appears in df1.
    
        Parameters:
        ----------
        df1 : pandas dataframe object
        df2 : pandas dataframe objct
        Pandas already offers df1.columns - df2.columns, but unfortunately
        the original order of the resulting columns is not maintained.
        """
        return [i for i in df1.columns if i not in df2.columns]
    
    
    def aj(df1, df2, overwriteColumns=True, inplace=False):
        """ KDB+ like asof join.
        Finds prevailing values of df2 asof df1's index. The resulting dataframe
        will have same number of rows as df1.
    
        Parameters
        ----------
        df1 : Pandas dataframe
        df2 : Pandas dataframe
        overwriteColumns : boolean, default True
             The columns of df2 will overwrite the columns of df1 if they have the same
             name unless overwriteColumns is set to False. In that case, this function
             will only join columns of df2 which are not present in df1.
        inplace : boolean, default False.
            If True, adds columns of df2 to df1. Otherwise, create a new dataframe with
            columns of both df1 and df2.
    
        *Assumes both df1 and df2 have datetime64 index. """
        joiner = lambda x : x.asof(df1.index)
        if not overwriteColumns:
            # Get columns of df2 not present in df1
            cols = diffCols(df2, df1)
            if len(cols) > 0:
                df2 = df2.ix[:,cols]
        result = df2.apply(joiner)
        if inplace:
            for i in result.columns:
                df1[i] = result[i]
            return df1
        else:
            return result
    

    Internally, this uses pandas.Series.asof().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pandas dataframe object that looks like this: one two three four
I have two queries that find both the Zip codes, and the States for
I have two pandas dataframes: dfLeft and dfRight with the date as the index.
Assume I have a pandas DataFrame with two columns, A and B. I'd like
I have a dataframe that looks like this: <class 'pandas.core.frame.DataFrame'> DatetimeIndex: 2016910 entries, 2009-01-02
I have two pandas DataFrames - weight has a simple Index on a Land
I have pandas DataFrame. I would like to get single value from a column
I have two classes (MVC view model) which inherits from one abstract base class.
Have two tables say ABC and XYZ and contain one column which data will
I have two lists, let's say: a = [1,2,3] b = [1,2,3,1,2,3] I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.