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

  • Home
  • SEARCH
  • 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 6774195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:45:29+00:00 2026-05-26T15:45:29+00:00

Is there a function in numpy that determines whether strings should be integers or

  • 0

Is there a function in numpy that determines whether strings should be integers or floating point numbers and automatically converts them? For instance, I often have a collection of records which are parsed from a text file using a combination of str.strip() and str.split(). Then I get something like

List = [['1','a','.3'],
        ['2','b','-.5']]

Which is then converted using numpy.rec.fromrecords:

In [1227]: numpy.rec.fromrecords(List)
Out[1227]: 
rec.array([('1', 'a', '.3'), ('2', 'b', '-.5')], 
      dtype=[('f0', '|S1'), ('f1', '|S1'), ('f2', '|S3')])

In R, there is a function called type.convert to which vectors/columns of character strings are passed and it will determine what the type for the column should be (i.e. if it’s a mix of strings and numbers it will remain a character vector). Excel does this also (based on its first 6 elements, if I recall correctly)…

Is there such a function in NumPy/Python? I know I could probably write a function to test whether each element of a column could be converted to an integer, etc., but is there anything built in? I know in all the examples the prescription is to specify the dtypes explicitly, but I would like to skip this step. Thanks.

  • 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-26T15:45:30+00:00Added an answer on May 26, 2026 at 3:45 pm

    numpy.genfromtxt can guess dtypes if you set dtype=None:

    import numpy as np
    import io
    
    alist = [['1','a','.3'],
            ['2','b','-.5']]
    
    f = io.BytesIO('\n'.join(' '.join(row) for row in alist))
    arr = np.genfromtxt(f,dtype=None)
    print(arr)
    print(arr.dtype)
    # [(1, 'a', 0.3) (2, 'b', -0.5)]
    # [('f0', '<i4'), ('f1', '|S1'), ('f2', '<f8')]
    

    Note that it would be better to apply np.genfromtxt directly to your text file instead of creating the intermediate list List (or what I called alist). If you need to do some processing of the file before sending it to np.genfromtxt, you could make a file-like object wrapper around the file which can do the processing and be passed to np.genfromtxt.

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

Sidebar

Related Questions

Is there any function that converts an escaped Url string to its unescaped form?
In numpy there is a function that makes arrays print prettier. set_printoptions(suppress = True)
Is there a function in Common Lisp that takes a string as an argument
Is there a function, that clears last line in command prompt? I dont mean
I have a function foo that takes a NxM numpy array as an argument
In NumPy functions, there are often initial lines that do checking of variable types,
Question: I've profiled my Python program to death, and there is one function that
I realize that there is a griddata for NumPy via Matplotlib , but is
is there a builtin function of Python that does on python.array what argsort() does
Is there a function like document.getElementById(FirstDiv).clear() ?

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.