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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:07:24+00:00 2026-06-12T18:07:24+00:00

I have a data structure that looks like this: data = [ (‘a’, 1.0,

  • 0

I have a data structure that looks like this:

data = [ ('a', 1.0, 2.0),
         ('b', 2.0, 4.0),
         ('c', 3.0, 6.0) ]

I want to convert it into a structured array using numpy. However, when I try the following, I keep the floats but I lose the string information:

import numpy
x = numpy.array(data, dtype=[('label', str), ('x', float), ('y', float)])
print x

Resulting in:

>>> [('', 1.0, 2.0) ('', 2.0, 4.0) ('', 3.0, 6.0)]

Could anyone explain why this happens, and how I might keep the string information?

  • 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-12T18:07:26+00:00Added an answer on June 12, 2026 at 6:07 pm

    You can see the problem if you print out the array and look carefully:

    >>> numpy.array(data, dtype=[('label', str), ('x', float), ('y', float)])
    array([('', 1.0, 2.0), ('', 2.0, 4.0), ('', 3.0, 6.0)], 
          dtype=[('label', '|S0'), ('x', '<f8'), ('y', '<f8')])
    

    The first field has a data type of '|S0' — a zero width string field. Make the string field longer — here’s a 2-char string field:

    >>> numpy.array(data, dtype=[('label', 'S2'), ('x', float), ('y', float)])
    array([('a', 1.0, 2.0), ('b', 2.0, 4.0), ('c', 3.0, 6.0)], 
          dtype=[('label', '|S2'), ('x', '<f8'), ('y', '<f8')])
    

    You can also do it this way, as documented here:

    >>> numpy.array(data, dtype=[('label', (str, 2)), ('x', float), ('y', float)])
    array([('a', 1.0, 2.0), ('b', 2.0, 4.0), ('c', 3.0, 6.0)], 
          dtype=[('label', '|S2'), ('x', '<f8'), ('y', '<f8')])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data structure that looks like this: data =[ {'key_1': { 'calc1':
I have a data structure that looks like this: Model Place primary key id
I have a data structure (created from a YAML document) that looks like this:
I currently have an XML Structure that looks something like this <Parent> <Info> <Info-Data></Info-Data>
I have a data-structure (in plist) that looks something like this: What i have
I have a data structure that looks like this (simplified version) class A(models.Model): a
I have a data structure that looks like Array ( [0] => Array (
I have a data structure that looks like this: [ {:choices=>[Hello, Hi]}, , {:choices=>[wor,
I have a data structure that looks like this: @results['events'].each do |event| event.inspect end
I have a data structure that looks like this: public class Node { public

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.