You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中分组求平均后float转数组及UTM转换报错解决

Fixing the TypeError When Converting Grouped Lat/Lon to UTM Coordinates

Hey there! Let's break down why you're getting that TypeError and how to fix it quickly.

The Root Cause

When you run df2.groupby(['record_time'])['lat'].mean(), the result dflat (and dflon) is a Pandas Series object, not a raw numerical array/list. The Proj function expects inputs like numpy arrays, lists, tuples, or scalars—so passing a Series directly triggers the type mismatch error.

Easy Solutions to Convert Series to Arrays

Here are a few straightforward ways to get the numerical array you need:

1. Use .to_numpy() (Recommended)

Pandas recommends using .to_numpy() for explicit conversion to a numpy array. It’s clear and works consistently across different data types:

myProj = Proj(init='epsg:32613')
# Convert Series to numpy arrays
lat_array = dflat.to_numpy()
lon_array = dflon.to_numpy()
# Now pass to Proj
x1, y1 = myProj(lon_array, lat_array)
print(x1, y1)

2. Use .values (Legacy but Still Works)

If you prefer a shorter syntax, .values will also return the underlying numpy array from the Series:

x1, y1 = myProj(dflon.values, dflat.values)

Note: .values can sometimes return unexpected types (like object arrays if there are NaNs), so .to_numpy() is safer for most cases.

3. Convert During Grouping (One-Step)

You can even convert to an array right when you calculate the mean, cutting out an extra step:

# Calculate mean and convert to array immediately
dflat = df2.groupby(['record_time'])['lat'].mean().to_numpy()
dflon = df2.groupby(['record_time'])['lon'].mean().to_numpy()

# Now use directly with Proj
x1, y1 = myProj(dflon, dflat)

Bonus: Handle Missing Values

If your grouped means have any NaN values, they might cause issues with the projection. Add .dropna() to clean them up before converting:

dflat = df2.groupby(['record_time'])['lat'].mean().dropna().to_numpy()
dflon = df2.groupby(['record_time'])['lon'].mean().dropna().to_numpy()

内容的提问来源于stack exchange,提问作者Mahsa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:48:34