How do I parse a metar csv file? #2812
-
Hello All, which has the structure station valid metar How do I use metpy to run this file and create a dataframe for all the rows in one go? Currently, I am iterating through all the rows and appending parsed data into a dataframe one by one which is taking a lot of time. i tried io.parse_metar_to_dataframe(den_df['metar']) but that doesn't work. thanks, |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 15 replies
-
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 We have an internal function |
Beta Was this translation helpful? Give feedback.
-
Looks like I still have to iterate over my csv file and parse metars one by one. Just that instead of appending to a dataframe I append to a list and then convert to a dataframe. Am I right? Isn't there a way that I can pass the dataframe of metars and get a dataframe of parsed metars back? |
Beta Was this translation helpful? Give feedback.
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:
Appending to a Python list and then turning that into a
DataFrame
will avoid the O(N^2) behavior.