Skip to content

Parsing error when reading cxml file in tc_tracks_forecast.read_cxml() #172

Description

@hannahker

In trying to read a cxml file recently downloaded from NCAR (see here), I hit a parsing error.

Looks like this is happening because the cycloneNumber is now a string, rather than the int that is expected. My guess is this line needs to be changed from Int64 to object.

My code:

tc_tracks_forecast.TCForecast.read_cxml(
    cxml_path="data/xml/z_tigge_c_ecmf_20240822120000_ifs_glob_prod_all_glo.xml",
)

Error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
File lib.pyx:2374, in pandas._libs.lib.maybe_convert_numeric()

ValueError: Unable to parse string "01C"

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
Cell In[4], line 1
----> 1 tc_tracks_forecast.TCForecast.read_cxml(
      2     cxml_path="data/xml/z_tigge_c_ecmf_20240822120000_ifs_glob_prod_all_glo.xml"
      3 )

File ~/miniforge3/envs/climada_env/lib/python3.11/site-packages/climada_petals/hazard/tc_tracks_forecast.py:609, in TCForecast.read_cxml(cls, cxml_path, xsl_path)
    592 @classmethod
    593 def read_cxml(cls, cxml_path: str, xsl_path: str=None):
    594     """Reads a cxml (cyclone xml) file and returns a class instance.
    595 
    596     Parameters
   (...)
    607         TCTracks with data from the given cxml file.
    608     """
--> 609     df = cls._cxml_to_df(cxml_path=cxml_path, xsl_path=xsl_path)
    610     df_groupby = df.groupby(
    611         ["disturbance_no", "baseTime", "basin", "cycloneNumber", "member"],
    612         sort=False,
    613         dropna=False,
    614     )
    615     instance = cls()

File ~/miniforge3/envs/climada_env/lib/python3.11/site-packages/climada_petals/hazard/tc_tracks_forecast.py:635, in TCForecast._cxml_to_df(cxml_path, xsl_path, basin_env_pressures)
    632 transformer = et.XSLT(xsl)
    633 csv_string = str(transformer(xml))
--> 635 all_storms_df = pd.read_csv(
    636     io.StringIO(csv_string),
    637     dtype={
    638         "member": "Int64",
    639         "cycloneNumber": "Int64",
    640         "hour": "Int64",
    641         "cycloneName": "object",
    642         "id": "object",
    643     },
    644     parse_dates=["baseTime", "validTime"],
    645     infer_datetime_format=True,
    646 )
    648 all_storms_df.dropna(
    649     subset=["validTime", "latitude", "longitude"], how="any", inplace=True
    650 )
    652 if basin_env_pressures is None:

File ~/miniforge3/envs/climada_env/lib/python3.11/site-packages/pandas/io/parsers/readers.py:948, in read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, date_format, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, encoding_errors, dialect, on_bad_lines, delim_whitespace, low_memory, memory_map, float_precision, storage_options, dtype_backend)
    935 kwds_defaults = _refine_defaults_read(
    936     dialect,
    937     delimiter,
   (...)
    944     dtype_backend=dtype_backend,
    945 )
    946 kwds.update(kwds_defaults)
--> 948 return _read(filepath_or_buffer, kwds)

File ~/miniforge3/envs/climada_env/lib/python3.11/site-packages/pandas/io/parsers/readers.py:617, in _read(filepath_or_buffer, kwds)
    614     return parser
    616 with parser:
--> 617     return parser.read(nrows)

File ~/miniforge3/envs/climada_env/lib/python3.11/site-packages/pandas/io/parsers/readers.py:1748, in TextFileReader.read(self, nrows)
   1741 nrows = validate_integer("nrows", nrows)
   1742 try:
   1743     # error: "ParserBase" has no attribute "read"
   1744     (
   1745         index,
   1746         columns,
   1747         col_dict,
-> 1748     ) = self._engine.read(  # type: ignore[attr-defined]
   1749         nrows
   1750     )
   1751 except Exception:
   1752     self.close()

File ~/miniforge3/envs/climada_env/lib/python3.11/site-packages/pandas/io/parsers/c_parser_wrapper.py:234, in CParserWrapper.read(self, nrows)
    232 try:
    233     if self.low_memory:
--> 234         chunks = self._reader.read_low_memory(nrows)
    235         # destructive to chunks
    236         data = _concatenate_chunks(chunks)

File parsers.pyx:843, in pandas._libs.parsers.TextReader.read_low_memory()

File parsers.pyx:920, in pandas._libs.parsers.TextReader._read_rows()

File parsers.pyx:1065, in pandas._libs.parsers.TextReader._convert_column_data()

File parsers.pyx:1104, in pandas._libs.parsers.TextReader._convert_tokens()

File parsers.pyx:1210, in pandas._libs.parsers.TextReader._convert_with_dtype()

File ~/miniforge3/envs/climada_env/lib/python3.11/site-packages/pandas/core/arrays/numeric.py:275, in NumericArray._from_sequence_of_strings(cls, strings, dtype, copy)
    269 @classmethod
    270 def _from_sequence_of_strings(
    271     cls, strings, *, dtype: Dtype | None = None, copy: bool = False
    272 ) -> Self:
    273     from pandas.core.tools.numeric import to_numeric
--> 275     scalars = to_numeric(strings, errors="raise", dtype_backend="numpy_nullable")
    276     return cls._from_sequence(scalars, dtype=dtype, copy=copy)

File ~/miniforge3/envs/climada_env/lib/python3.11/site-packages/pandas/core/tools/numeric.py:222, in to_numeric(arg, errors, downcast, dtype_backend)
    220 coerce_numeric = errors not in ("ignore", "raise")
    221 try:
--> 222     values, new_mask = lib.maybe_convert_numeric(  # type: ignore[call-overload]  # noqa: E501
    223         values,
    224         set(),
    225         coerce_numeric=coerce_numeric,
    226         convert_to_masked_nullable=dtype_backend is not lib.no_default
    227         or isinstance(values_dtype, StringDtype)
    228         and not values_dtype.storage == "pyarrow_numpy",
    229     )
    230 except (ValueError, TypeError):
    231     if errors == "raise":

File lib.pyx:2416, in pandas._libs.lib.maybe_convert_numeric()

ValueError: Unable to parse string "01C" at position 0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions