Skip to content

Instantly share code, notes, and snippets.

@tommylees112
Created April 8, 2021 12:33
Show Gist options
  • Save tommylees112/69fdcbd1139e380429f6a501f47a35a0 to your computer and use it in GitHub Desktop.
Save tommylees112/69fdcbd1139e380429f6a501f47a35a0 to your computer and use it in GitHub Desktop.
example xarray to pandas conversion
import xarray as xr
from pathlib import Path
if __name__ == "__main__":
data_dir = Path("/Users/tommylees/github/spatio_temporal/data")
# list(data_dir.glob("*.nc"))
ds = xr.open_dataset(data_dir / "camels_static.nc")
df = ds.to_dataframe()
print(df.head())
@tommylees112
Copy link
Author

An example of how to use the xarray .to_dataframe() functionality. The reality is that most often you want netcdf format files to remain in xarray as xr.DataArray or xr.Dataset objects. But there are cases where it can make sense to convert to pd.DataFrame objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment