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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:55:57+00:00 2026-06-11T08:55:57+00:00

I had a similar question some days ago that was solved, but now, some

  • 0

I had a similar question some days ago that was solved, but now, some of my files have a very similar file, but where the header has a space before the name, or a “.” in the end and it just doesn’t work.

So, I have this data1:

 Year,Day,Hour,Min,Sec.,P1S1
 2003,  1, 0, 1,30.09, 0.295E+04
 2003,  1, 1, 0,11.84, 0.297E+04
 2003,  1, 2, 0, 8.26, 0.338E+04
 2003,  1, 3, 0, 4.69, 0.291E+04
 2003,  1, 4, 0, 1.11, 0.337E+04

And I can read it with (notice the need of a space before Year in ‘ Year’, that is needed to read the file!):

import pandas as pd

def parse(yr, doy, hr, min, sec):
    yr, doy, hr, min = [int(x) for x in [yr, doy, hr, min]]
    sec = float(sec)
    mu_sec = int((sec - int(sec)) * 1e6)
    sec = int(sec)
    dt = datetime(yr - 1, 12, 31)
    delta = timedelta(days=doy, hours=hr, minutes=min, seconds=sec, microseconds=mu_sec)

    return dt + delta

# notice the need of a space before Year in ' Year', that is needed to read the file!
pd.read_csv(data1, parse_dates=[[' Year','Day','Hour','Min','Sec.']], date_parser=parse, index_col=0)

Now, if I try the same with, data2 (notice that now there is a ‘.’ after Min that didn’t exist in data1):

 Year,Day,Hour,Min.,Sec.,P1S1
 2003,  1, 0, 0, 0.00, 0.261E+04
 2003,  1, 0, 5, 0.00, 0.281E+04
 2003,  1, 0,10, 0.00, 0.268E+04
 2003,  1, 0,15, 0.00, 0.305E+04

When I do:

pd.read_csv(data2, parse_dates=[[' Year','Day','Hour','Min','Sec.']], date_parser=parse, index_col=0)

I get an error because Python/Pandas is not expecting that ‘.’ after ‘Min’, or the same when I have a file without the space before ‘Year’. Or any other slight difference in those first 5 header field names.

So, my question is, is there any way to make this more robust? I know the first 5 fields are always in this format, it’s just their name in the header that changes.

  • 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-11T08:55:58+00:00Added an answer on June 11, 2026 at 8:55 am

    If you know they’re always in the same positions you can say something like parse_dates=[[0,1,2,3,4]].

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

Sidebar

Related Questions

I had asked a similar question a few days ago but think I was
I had a similar question before, but this is a few steps beyond that
I have found some similar Que's on SO but had not find the solution.
[I posted a similar question on serverfault.com, but had very little feedback so trying
I asked a similar question yesterday but I included some code that basically took
I had a similar question a while back, but with much less of a
I had asked a question very much similar to this in the thread: https://stackoverflow.com/questions/11259474/store-the-numericals-in-char-array-into-an-integer-variable-in-vc
I recently had a similar thread about this, but now I need to animate
i have seen similar posts here but i have had no luck solving my
Can someone help me please. I had replies to a similar question but am

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.