Tip

For an interactive online version click here: Binder badge

Example: Reading 2D tabular data (DataFrame)#

This example illustrates the how to read 2D tabular data using the HydroMT DataCatalog with the csv driver.

[1]:
# import hydromt and setup logging
from hydromt import DataCatalog
from hydromt.log import setuplog

logger = setuplog("tabular data example", log_level=10)
2023-07-18 14:02:36,683 - tabular data example - log - INFO - HydroMT version: 0.8.0
[2]:
data_catalog = DataCatalog("data/tabular_data_catalog.yml", logger=logger)
2023-07-18 14:02:36,693 - tabular data example - data_catalog - INFO - Parsing data catalog from data/tabular_data_catalog.yml

CSV driver#

time series data#

To read 2D tabular data from a comma-separated file (csv) and parse it into a pandas.DataFrame we use the pandas.read_csv(). Any driver_kwargs in the data catalog are passed to this method, e.g., parsing dates in the “time” colum and setting this as the index.

This works similarly for excel tables, but based on the pandas.read_excel() method.

For demonstration we use a dummy example timeseries data in csv.

[3]:
# inspect data source entry in data catalog yaml file
data_catalog["example_csv_data"]
[3]:
data_type: DataFrame
driver: csv
driver_kwargs:
  index_col: time
  parse_dates: true
filesystem: local
path: /home/runner/work/hydromt/hydromt/docs/_examples/data/example_csv_data.csv

We can load any 2D tabular data using DataCatalog.get_dataframe(). Note that if we don’t provide any arguments it returns the full dataframe.

[4]:
df = data_catalog.get_dataframe("example_csv_data")
df.head()
2023-07-18 14:02:36,713 - tabular data example - data_catalog - INFO - DataCatalog: Getting example_csv_data DataFrame csv data from /home/runner/work/hydromt/hydromt/docs/_examples/data/example_csv_data.csv
2023-07-18 14:02:36,714 - tabular data example - dataframe - INFO - DataFrame: Read csv data.
[4]:
col1 col2
time
2016-01-01 0.590860 0.591380
2016-01-02 0.565552 0.571342
2016-01-03 0.538679 0.549770
2016-01-04 0.511894 0.526932
2016-01-05 0.483989 0.502907

The data can be visualized with the .plot() pandas method.

[5]:
df.plot(y="col1")
[5]:
<Axes: xlabel='time'>
../_images/_examples_reading_tabular_data_11_1.png

reclassification table#

Another typical usecase for tabular data are reclassification tables to reclassify e.g. land use data to manning roughness. An example of this data is shown in the cells below. Note tha the values are not validated and likely too high!

[6]:
# read both the vito_reclass and artifact_data data catalogs
data_catalog = DataCatalog(["data/vito_reclass.yml", "artifact_data"], logger=logger)
data_catalog["vito_reclass"]
2023-07-18 14:02:37,479 - tabular data example - data_catalog - INFO - Parsing data catalog from data/vito_reclass.yml
2023-07-18 14:02:37,538 - tabular data example - data_catalog - INFO - Reading data catalog artifact_data v0.0.8 from archive
2023-07-18 14:02:37,539 - tabular data example - data_catalog - INFO - Parsing data catalog from /home/runner/.hydromt_data/artifact_data/v0.0.8/data_catalog.yml
[6]:
data_type: DataFrame
driver: csv
driver_kwargs:
  index_col: 0
filesystem: local
meta:
  notes: reclass table for manning values
path: /home/runner/work/hydromt/hydromt/docs/_examples/data/vito_reclass.csv
[7]:
df = data_catalog.get_dataframe("vito_reclass")
df.head()
2023-07-18 14:02:37,637 - tabular data example - data_catalog - INFO - DataCatalog: Getting vito_reclass DataFrame csv data from /home/runner/work/hydromt/hydromt/docs/_examples/data/vito_reclass.csv
2023-07-18 14:02:37,640 - tabular data example - dataframe - INFO - DataFrame: Read csv data.
[7]:
description manning
vito
0 Unknown -999.000
20 Shrubs 0.500
30 Herbaceous vegetation 0.150
40 Cultivated and managed vegetation/agriculture ... 0.200
50 Urban / built up 0.011
[8]:
da_lulc = data_catalog.get_rasterdataset("vito")
da_man = da_lulc.raster.reclassify(df[["manning"]])
da_man["manning"].plot.imshow()
2023-07-18 14:02:37,655 - tabular data example - data_catalog - INFO - DataCatalog: Getting vito RasterDataset raster data from /home/runner/.hydromt_data/artifact_data/v0.0.8/vito.tif
[8]:
<matplotlib.image.AxesImage at 0x7f35818454e0>
../_images/_examples_reading_tabular_data_16_2.png