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 7974819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:29:18+00:00 2026-06-04T08:29:18+00:00

I have periodic data with the index being a floating point number like so:

  • 0

I have periodic data with the index being a floating point number like so:

time =    [0, 0.1, 0.21, 0.31, 0.40, 0.49, 0.51, 0.6, 0.71, 0.82, 0.93]
voltage = [1,  -1,  1.1, -0.9,    1,   -1,  0.9,-1.2, 0.95, -1.1, 1.11]
df = DataFrame(data=voltage, index=time, columns=['voltage'])
df.plot(marker='o')

I want to create a cross(df, y_val, direction='rise' | 'fall' | 'cross') function that returns an array of times (indexes) with all the
interpolated points where the voltage values equal y_val. For ‘rise’ only the values where the slope is positive are returned; for ‘fall’ only the values with a negative slope are retured; for ‘cross’ both are returned. So if y_val=0 and direction=’cross’ then an array with 10 values would be returned with the X values of the crossing points (the first one being about 0.025).

I was thinking this could be done with an iterator but was wondering if there was a better way to do this.

Thanks. I’m loving Pandas and the Pandas community.

  • 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-04T08:29:19+00:00Added an answer on June 4, 2026 at 8:29 am

    To do this I ended up with the following. It is a vectorized version which is 150x faster than one that uses a loop.

    def cross(series, cross=0, direction='cross'):
        """
        Given a Series returns all the index values where the data values equal 
        the 'cross' value. 
    
        Direction can be 'rising' (for rising edge), 'falling' (for only falling 
        edge), or 'cross' for both edges
        """
        # Find if values are above or bellow yvalue crossing:
        above=series.values > cross
        below=np.logical_not(above)
        left_shifted_above = above[1:]
        left_shifted_below = below[1:]
        x_crossings = []
        # Find indexes on left side of crossing point
        if direction == 'rising':
            idxs = (left_shifted_above & below[0:-1]).nonzero()[0]
        elif direction == 'falling':
            idxs = (left_shifted_below & above[0:-1]).nonzero()[0]
        else:
            rising = left_shifted_above & below[0:-1]
            falling = left_shifted_below & above[0:-1]
            idxs = (rising | falling).nonzero()[0]
    
        # Calculate x crossings with interpolation using formula for a line:
        x1 = series.index.values[idxs]
        x2 = series.index.values[idxs+1]
        y1 = series.values[idxs]
        y2 = series.values[idxs+1]
        x_crossings = (cross-y1)*(x2-x1)/(y2-y1) + x1
    
        return x_crossings
    
    # Test it out:
    time = [0, 0.1, 0.21, 0.31, 0.40, 0.49, 0.51, 0.6, 0.71, 0.82, 0.93]
    voltage = [1,  -1,  1.1, -0.9,    1,   -1,  0.9,-1.2, 0.95, -1.1, 1.11]
    df = DataFrame(data=voltage, index=time, columns=['voltage'])
    x_crossings = cross(df['voltage'])
    y_crossings = np.zeros(x_crossings.shape)
    plt.plot(time, voltage, '-ob', x_crossings, y_crossings, 'or')
    plt.grid(True)
    

    It was quite satisfying when this worked. Any improvements that can be made?

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

Sidebar

Related Questions

Is there a way using Core Plot to have periodic labels assigned to data
I have an application that records data from a manufacturing process on a periodic
I have some periodic data, but the amount of data is not a multiple
Some facts about my data I have a TablePattern that defines periodic day pattern
Currently I have a background thread that does some periodic heavy data management. Because
I have a chat like app, and to receive periodic chats, I want to
I have a lot of data (all elemenents of the periodic table) in a
I have a SQL table with periodic measurements. I'd like to be able to
I have objects that have comments. As part of a periodic email summary, I
We're designing data import from an external source like MAS200 into our production SQL

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.