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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:28:37+00:00 2026-06-11T06:28:37+00:00

I’m porting a Matlab script to Python. Below is an extract: %// Create a

  • 0

I’m porting a Matlab script to Python. Below is an extract:

%// Create a list of unique trade dates
DateList = unique(AllData(:,1));

%// Loop through the dates
for DateIndex = 1:size(DateList,1)

    CalibrationDate = DateList(DateIndex);
    %// Extract the data for a single cablibration date (but all expiries)
    SubsetIndices = ismember(AllData(:,1) , DateList(DateIndex)) == 1;    
    SubsetAllExpiries = AllData(SubsetIndices, :);

AllData is an N-by-6 cell matrix, the first 2 columns are dates (strings) and the other 4 are numbers. In python I will be getting this data out of a csv so something like this:

import numpy as np
AllData = np.recfromcsv(open("MyCSV.csv", "rb"))

So now if I’m not mistaken AllData is a numpy array of ordinary tuples. Is this is best format to have this data in? The goal will be to extract a list of unique dates from column 1, and for each date extract the rows with that date in column 1 (column one is ordered). Then for each row in column one do some maths on the numbers and date in the remaining 5 columns.

So in matlab I can get the list of dates by unique(AllData(:,1)) and then I can get the records (rows) corresponding to that date (i.e. with that date in columns one) like this:

SubsetIndices = ismember(AllData(:,1) , MyDate) == 1;    
SubsetAllExpiries = AllData(SubsetIndices, :);

How can I best achieve the same results in Python?

  • 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-11T06:28:39+00:00Added an answer on June 11, 2026 at 6:28 am

    To put things in context, np.recfromcsv is just a modified version of np.genfromtxt which outputs record arrays instead of structured arrays.

    A structured array lets you access the individual fields (here, your columns) by their names, like in my_array["field_one"] while a record array gives you the same plus the possibility to access the fields as attributes, like in my_array.field_one. I’m not fond of “access-as-attributes”, so I usually stick to structured arrays.

    For your information, structurede/record arrays are not arrays of tuples, but arrays of some numpy object call a np.void: it’s a block of memory composed of as many sub-blocks you have of fields, the size of each sub-block depending on its datatype.

    That said, yes, what you seem to have in mind is exactly the kind of usage for a structured array. The approach would then be:

    • to take your dates array and filter them to find the unique elements.
    • to find the indices of these unique elements, as an array of integers we’ll call, say, matching;
    • to use matching to access the corresponding records (eg, rows of your array) using fancy indexing, as
      my_array[matching].
    • to perform your computations on the records, as you want.

    Note that you can keep your dates as strings or transform them into datetime objects using a user-defined converter, as described in the documentation. For example, your could transform a YYYY-MM-DD into a datetime object with a lambda s:datetime.dateime.strptime(s,"%Y-%m-%d"). That way, instead of having, say, a N array where each row (a record) consists of two dates as strings and 4 floats, you would have a N array where each row consists of two datetime objects and 4 floats.

    Note the shape of your array (via my_array.shape), it says (N,), meaning it’s a 1D array, even if it looks like a 2D table with multiple columns. You can access individual fields (each “column”) by using its name. For example, if we create an array consisting of one string field called first and one int field called second, like that:

    x = np.array([('a',1),('b',2)], dtype=[('first',"|S10"),('second',int)])
    

    you could access the first column with

    >>> x['first']
    array(['a', 'b'], 
          dtype='|S10')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.