pandas 使用panel 报错 Panel is deprecated and will be removed in a future version.
Panel is deprecated and will be removed in a future version.
The recommended way to represent these types of 3-dimensional data are with a MultiIndex on a DataFrame, via the Panel.to_frame() method
Alternatively, you can use the xarray package http://xarray.pydata.org/en/stable/.
Pandas provides a `.to_xarray()` method to help automate this conversion.
--------------------------------------
上面说的意思:panel在新版本中被xarray的包取代了。可以使用xarray包下的 to_xarray() 方法。
使用方法,例子:
----------官方文档--------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | pandas.Panel.to_xarray Panel.to_xarray()[source] Return an xarray object from the pandas object . Returns: xarray.DataArray or xarray.Dataset Data in the pandas structure converted to Dataset if the object is a DataFrame, or a DataArray if the object is a Series. See also DataFrame.to_hdf Write DataFrame to an HDF5 file . DataFrame.to_parquet Write a DataFrame to the binary parquet format . Notes See the xarray docs Examples >>> df = pd.DataFrame([( 'falcon' , 'bird' , 389.0 , 2 ), ... ( 'parrot' , 'bird' , 24.0 , 2 ), ... ( 'lion' , 'mammal' , 80.5 , 4 ), ... ( 'monkey' , 'mammal' , np.nan, 4 )], ... columns = [ 'name' , 'class' , 'max_speed' , ... 'num_legs' ]) >>> df name class max_speed num_legs 0 falcon bird 389.0 2 1 parrot bird 24.0 2 2 lion mammal 80.5 4 3 monkey mammal NaN 4 >>> df.to_xarray() <xarray.Dataset> Dimensions: (index: 4 ) Coordinates: * index (index) int64 0 1 2 3 Data variables: name (index) object 'falcon' 'parrot' 'lion' 'monkey' class (index) object 'bird' 'bird' 'mammal' 'mammal' max_speed (index) float64 389.0 24.0 80.5 nan num_legs (index) int64 2 2 4 4 >>> df[ 'max_speed' ].to_xarray() <xarray.DataArray 'max_speed' (index: 4 )> array([ 389. , 24. , 80.5 , nan]) Coordinates: * index (index) int64 0 1 2 3 >>> dates = pd.to_datetime([ '2018-01-01' , '2018-01-01' , ... '2018-01-02' , '2018-01-02' ]) >>> df_multiindex = pd.DataFrame({ 'date' : dates, ... 'animal' : [ 'falcon' , 'parrot' , 'falcon' , ... 'parrot' ], ... 'speed' : [ 350 , 18 , 361 , 15 ]}).set_index([ 'date' , ... 'animal' ]) >>> df_multiindex speed date animal 2018 - 01 - 01 falcon 350 parrot 18 2018 - 01 - 02 falcon 361 parrot 15 >>> df_multiindex.to_xarray() <xarray.Dataset> Dimensions: (animal: 2 , date: 2 ) Coordinates: * date (date) datetime64[ns] 2018 - 01 - 01 2018 - 01 - 02 * animal (animal) object 'falcon' 'parrot' Data variables: speed (date, animal) int64 350 18 361 15 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import xarray as xr a = pd.DataFrame({ 'a' :[1,2,3], 'b' :[4,5,6], 'c' :[7,8,9]}) b = pd.DataFrame({ 'a' :[11,12,13], 'b' :[14,15,16], 'c' :[17,18,19]}) ds = xr.Dataset({1:a, 2:b}) def f(thing): #print(thing) return thing.mean() >>> q = ds.apply(f) >>> q <xarray.Dataset> Dimensions: () Data variables: 1 float64 5.0 2 float64 15.0 >>> q[1] <xarray.DataArray 1 ()> array(5.) >>> q[1].values array(5.) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~