在熊猫中合并两个数据框时出现值错误

问题描述 投票:0回答:1

我尝试使用熊猫合并两个数据框,但这是我得到的错误代码:

ValueError:您正在尝试合并datetime64 [ns]和datetime64 [ns,UTC]列。如果您希望继续,则应使用pd.concat

我尝试了在线找到其他解决方案,但是没有任何效果!!该代码已提供给我,似乎可以在其他PC上使用,但不能在我的计算机上使用。

这是我的代码:

import sys
import os
from datetime import datetime
import numpy  as np
import pandas as pd


# --------------------------------------------------------------------
# -- price, consumption and production                              --
# --------------------------------------------------------------------

fn = '../data/np_data.csv'
if os.path.isfile(fn):
    df_data = pd.read_csv(fn,header=[0],parse_dates=[0])

else:
    sys.exit('Could not open data file {}̈́'.format(fn))


# --------------------------------------------------------------------
# -- temp. data                                               --
# --------------------------------------------------------------------

fn = '../data/temp.csv'
if os.path.isfile(fn):
    dtemp = pd.read_csv(fn,header=[0],parse_dates=[0])

else:
    sys.exit('Could not open data file {}̈́'.format(fn))


# --------------------------------------------------------------------
# --  price data                                              --
# --   first date: 2014-01-13                                       --
# --   last  date: 2020-02-01                                       --
# --------------------------------------------------------------------

fn = '../data/eprice.csv'
if os.path.isfile(fn):
    eprice = pd.read_csv(fn,header=[0])

else:
    sys.exit('Could not open data file {}̈́'.format(fn))


# --------------------------------------------------------------------
# -- combine dataframes (and save as CSV file)                      --
# --------------------------------------------------------------------

#

df= df_data.merge(dtemp, on='time',how='left')      ## This is where I get the error.

print(df.info())
print(eprice.info())

#
# add eprice
df = df.merge(eprice, on='date', how='left')

#
# eprice available only available on trading days
#   fills in missing values, last observation is used
  df = df.fillna(method='ffill')

#
# keep only the relevant time period
df = df[df.date > '2014-01-23']
df = df[df.date < '2020-02-01']


df.to_csv('../data/my_data.csv',index=False)

已导入的数据集看起来很正常,具有预期的列数和观察值。我在Panda中使用的版本是1.0.3

python pandas valueerror
1个回答
0
投票

尝试在加入之前在两个时间列上都执行df['Time'] = pd.to_datetime(df['Time'], utc = True)(或者更确切地说,没有UTC的时间需要经历!)

© www.soinside.com 2019 - 2024. All rights reserved.