Getting error seeming time variable coordinates cant be assigned

cdo showvar data_0.nc

cdi warning (cdfInqContents): Coordinates variable number can’t be assigned!
cdi warning (cdfInqContents): Coordinates variable date can’t be assigned!
cdi warning (cdfInqContents): Coordinates variable expver can’t be assigned!
u10 v10 t2m sst sp tp
cdo showname: Processed 6 variables [0.09s 53MB]

when i try to view the data in python,
Variable names in the NetCDF file:
[‘number’, ‘date’, ‘pressure_level’, ‘latitude’, ‘longitude’, ‘expver’, ‘spatial_ref’, ‘r’, ‘t’, ‘u’, ‘v’]

Coordinates in the dataset:
Coordinates:
number int64 8B …

  • date (date) int64 3kB 19940101 19940201 … 20231101 20231201
  • pressure_level (pressure_level) float64 8B 1e+03
  • latitude (latitude) float64 160B 25.25 25.0 24.75 … 21.0 20.75 20.5
  • longitude (longitude) float64 240B 109.8 110.0 110.2 … 116.8 117.0
    expver (date) <U4 6kB …
    spatial_ref int32 4B 0

it has date instead of time.

when i print date frame it looks like :
19940101 19940201 19940301 19940401 19940501 19940601 19940701 19940801
19940901 19941001 19941101 19941201 19950101 19950201 19950301 19950401
19950501 19950601 19950701 19950801 19950901 19951001 19951101 19951201
19960101 19960201 19960301 19960401 19960501 19960601 19960701 19960801
19960901 19961001 19961101 19961201 19970101 19970201 19970301 19970401
19970501 19970601 19970701 19970801 19970901 19971001 19971101 19971201
19980101 19980201 19980301 19980401 19980501 19980601 19980701 19980801
19980901 19981001 19981101 19981201 19990101 19990201 19990301 19990401
19990501 19990601 19990701 19990801 19990901 19991001 19991101 19991201
20000101 20000201 20000301 20000401 20000501 20000601 20000701 20000801
20000901 20001001 20001101 20001201 20010101 20010201 20010301 20010401
20010501 20010601 20010701 20010801 20010901 20011001 20011101 20011201
20020101 20020201 20020301 20020401 20020501 20020601 20020701 20020801
20020901 20021001 20021101 20021201 20030101 20030201 20030301 20030401
20030501 20030601 20030701 20030801 20030901 20031001 20031101 20031201
20040101 20040201 20040301 20040401 20040501 20040601 20040701 20040801
20040901 20041001 20041101 20041201 20050101 20050201 20050301 20050401
20050501 20050601 20050701 20050801 20050901 20051001 20051101 20051201
20060101 20060201 20060301 20060401 20060501 20060601 20060701 20060801
20060901 20061001 20061101 20061201 20070101 20070201 20070301 20070401
20070501 20070601 20070701 20070801 20070901 20071001 20071101 20071201
20080101 20080201 20080301 20080401 20080501 20080601 20080701 20080801
20080901 20081001 20081101 20081201 20090101 20090201 20090301 20090401
20090501 20090601 20090701 20090801 20090901 20091001 20091101 20091201
20100101 20100201 20100301 20100401 20100501 20100601 20100701 20100801

import xarray as xr
import pandas as pd
import os

Define paths

script_dir = os.path.dirname(file)
parent_dir = os.path.dirname(script_dir)
ncfile = ‘ERA5.global.1994-2023.Pre.tem.uvwind.press.sst.netcdf’

Define the directory and file paths

Data_file_path = os.path.join(parent_dir, f"Data\ERA5\{ncfile}\")
Output_file_path = os.path.join(parent_dir, “Output/”) # Added output path

File name

nc_file = f’{ncfile}.nc’

Load the NetCDF data using xarray

ds = xr.open_dataset(Data_file_path + nc_file)

Step 1: Extract ‘date’ and convert it to pandas datetime format

if ‘date’ in ds.coords:
date_values = ds.coords[‘date’].values
time_index = pd.to_datetime(date_values.astype(str), format=‘%Y%m%d’)

# Step 2: Replace 'date' coordinate with 'valid_time'
ds = ds.assign_coords(date=time_index)

# Rename 'date' to 'valid_time'
ds = ds.rename({'date': 'valid_time'})

else:
print(“‘date’ coordinate not found in the dataset”)

Step 3: Save the updated dataset to a new NetCDF file

Create the output directory if it doesn’t exist

if not os.path.exists(Output_file_path):
os.makedirs(Output_file_path)

Define the output NetCDF file path

output_nc_file = os.path.join(Output_file_path, f"{ncfile}_valid_time.nc")

print(f"Saving corrected file to: {output_nc_file}")
ds.to_netcdf(output_nc_file)

print(“File saved successfully!”)

I used this code and now i can use the data normally with pandas

Is there reason for changing the coordinate from time to date and removing the units? Or is this a bug that will be fixed one day?