Skip to content

How do I parse a metar csv file? #2812

Answered by dopplershift
ManojAgrawal asked this question in Q&A
Discussion options

You must be logged in to vote

Yeah, you definitely don't want to keep appending to a dataframe one-by-one because that (I believe) will result in making a copy of the entire dataframe every time you do that. This leads to O(N^2) behavior. Here's what MetPy's code does, which should work for your case:

import contextlib

from metpy.io.metar import ParseError
import pandas as pd

metars = []
for metar in df['metar']:
    with contextlib.suppress(ParseError):
        # Parse the string of text and assign to values within the named tuple
        metars.append(parse_metar(metar, year=year, month=month))
df = pd.DataFrame(metars)

Appending to a Python list and then turning that into a DataFrame will avoid the O(N^2) behavior.

Replies: 2 comments 15 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by dopplershift
Comment options

You must be logged in to vote
15 replies
@ManojAgrawal
Comment options

@akrherz
Comment options

@ManojAgrawal
Comment options

@dopplershift
Comment options

@ManojAgrawal
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
Area: IO Pertains to reading data
3 participants