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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:34:03+00:00 2026-05-16T15:34:03+00:00

I have a 2d numpy.array, where the first column contains datetime.datetime objects, and the

  • 0

I have a 2d numpy.array, where the first column contains datetime.datetime objects, and the second column integers:

A = array([[2002-03-14 19:57:38, 197],
       [2002-03-17 16:31:33, 237],
       [2002-03-17 16:47:18, 238],
       [2002-03-17 18:29:31, 239],
       [2002-03-17 20:10:11, 240],
       [2002-03-18 16:18:08, 252],
       [2002-03-23 23:44:38, 327],
       [2002-03-24 09:52:26, 334],
       [2002-03-25 16:04:21, 352],
       [2002-03-25 18:53:48, 353]], dtype=object)

What I would like to do is select all rows for a specific date, something like

A[first_column.date()==datetime.date(2002,3,17)]
array([[2002-03-17 16:31:33, 237],
           [2002-03-17 16:47:18, 238],
           [2002-03-17 18:29:31, 239],
           [2002-03-17 20:10:11, 240]], dtype=object)

How can I achieve this?

Thanks for your insight 🙂

  • 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-05-16T15:34:04+00:00Added an answer on May 16, 2026 at 3:34 pm

    You could do this:

    from_date=datetime.datetime(2002,3,17,0,0,0)
    to_date=from_date+datetime.timedelta(days=1)
    idx=(A[:,0]>from_date) & (A[:,0]<=to_date)
    print(A[idx])
    # array([[2002-03-17 16:31:33, 237],
    #        [2002-03-17 16:47:18, 238],
    #        [2002-03-17 18:29:31, 239],
    #        [2002-03-17 20:10:11, 240]], dtype=object)
    

    A[:,0] is the first column of A.

    Unfortunately, comparing A[:,0] with a datetime.date object raises a TypeError. However, comparison with a datetime.datetime object works:

    In [63]: A[:,0]>datetime.datetime(2002,3,17,0,0,0)
    Out[63]: array([False,  True,  True,  True,  True,  True,  True,  True,  True,  True], dtype=bool)
    

    Also, unfortunately,

    datetime.datetime(2002,3,17,0,0,0)<A[:,0]<=datetime.datetime(2002,3,18,0,0,0)
    

    raises a TypeError too, since this calls datetime.datetime‘s __lt__ method instead of the numpy array’s __lt__ method. Perhaps this is a bug.

    Anyway, it’s not hard to work-around; you can say

    In [69]: (A[:,0]>datetime.datetime(2002,3,17,0,0,0)) & (A[:,0]<=datetime.datetime(2002,3,18,0,0,0))
    Out[69]: array([False,  True,  True,  True,  True, False, False, False, False, False], dtype=bool)
    

    Since this gives you a boolean array, you can use it as a “fancy index” to A, which yields the desired result.

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

Sidebar

Related Questions

I'v got two numpy arrays. The first array contains some zeros (which are distributed
I have a 2D array of Numpy data read from a .csv file. Each
How does one add rows to a numpy array? I have an array A:
I have the following code in Python using Numpy: p = np.diag(1.0 / np.array(x))
I have a numpy array of pixel data that I want to draw at
I have a numpy.array of 640x480 images, each of which is 630 images long.
I have a multidimensional numpy array, and I need to iterate across a given
I have a 2D numpy array. Is there a way to create a view
Assuming I have a numpy array like: [1,2,3,4,5,6] and another array: [0,0,1,2,2,1] I want
I have a numpy array: A = array([['id1', '1', '2', 'NaN'], ['id2', '2', '0',

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.