简体   繁体   English

Python:用日期时间值替换 dataframe 中的列

[英]Python : Replace a column in a dataframe by datetime values

I'm trying to replace a column an array of 4 columns by datetime values that I treated.我正在尝试用我处理的日期时间值替换一列 4 列的数组。 The problem is that it's difficult to keep the same form between the different formats of dataframe, array,....问题是很难在 dataframe、数组、.... 的不同格式之间保持相同的形式。


dataw = ds.variables["pr"][:]
dataw = np.array(dataw[:,0,0])
lat = ds.variables["lat"][:]
long = ds.variables["lon"][:]
time = ds.variables["time"][:]

time = pd.to_datetime(ds.variables["time"][:],origin=pd.Timestamp('1850-01-01'),unit='D')
#np.datetime64(ds.variables["time"][:],'D')
x2 = pd.DataFrame(np.zeros((len(dataw),4), float))
x = np.zeros((len(dataw),4), float)


x[:,0] = time
x[:,1] = long
x[:,2] = lat[:]
x[:,3] = dataw[:]*86400


x=pd.DataFrame(x)
x[:,0] = pd.to_datetime(time,origin=pd.Timestamp('1850-01-01'),unit='D')

If I put directly the dates transformed in the array, the result is like: 1.32542e+18如果我直接将转换后的日期放入数组中,结果如下:1.32542e+18

I tried我试过

time = ds.variables["time"][:]

and include it in the array, and then use并将其包含在数组中,然后使用

x[:,0]=pd.to_datetime(x[:,0],origin=pd.Timestamp('1850-01-01'),unit='D')

I get the error:我收到错误:

TypeError: unhashable type: 'slice'

I tried also directly put:我试过也直接放:

time=pd.to_datetime(time,origin=pd.Timestamp('1850-01-01'),unit='D')
x[:,0] = time[:]
TypeError: unhashable type: 'slice'

try this instead试试这个

        import numpy as np
    import pandas as pd
    
    dataw = ds.variables["pr"][:]
    dataw = np.array(dataw[:, 0, 0])
    lat = ds.variables["lat"][:]
    long = ds.variables["lon"][:]
    time = ds.variables["time"][:]
    
    time = np.datetime64(time, 'D')
x = np.zeros((len(dataw), 4), dtype='datetime64[D]')
    x[:, 0] = time
    x[:, 1] = long
    x[:, 2] = lat
    x[:, 3] = dataw * 86400
    
    df = pd.DataFrame(x, columns=["Time", "Longitude", "Latitude", "Data"])

Xarray makes the.netcdf->pandas workflow quite straightforward: Xarray 使 .netcdf->pandas 工作流程非常简单:

import xarray as xr

ds = xr.open_dataset('file.nc', engine='netcdf4')
df = ds.to_pandas()

Presuming your time variable is using cf-conventions, Xarray will automatically decode it into datetime objects.假设您的时间变量使用 cf-conventions,Xarray 会自动将其解码为 datetime 对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM