感谢这个论坛上的其他人,我有这个代码:
names=['Date','Wind Speed','Wind Direction']
df2 = pd.read_csv('test_met.csv', index_col=0, names=names, parse_dates=[0])
aethalometer=['Date','Conc']
df1=pd.read_csv('BC_2012_1min.csv', index_col=0, names=aethalometer, parse_dates=[0])
df1=df1[df1['Conc']>-10]
print(len(df1))
print("here")
df1.index = df1.index.to_period('h')
df2['per'] = df2.index.to_period('h')
pers = df2.loc[(df2['Wind Direction'] > 340) | (df2['Wind Direction'] < 12) , 'per'].unique()
现在有了这个:我得到:
TypeError: unorderable types: str() > int()
打印 df1.index:
我得到:
Index(['TimeW_1min', '01/04/2012 00:00', '01/04/2012 00:01',
'01/04/2012 00:02', '01/04/2012 00:03', '01/04/2012 00:04',
'01/04/2012 00:05', '01/04/2012 00:06', '01/04/2012 00:07',
'01/04/2012 00:08',
...
'30/09/2012 23:50', '30/09/2012 23:51', '30/09/2012 23:52',
'30/09/2012 23:53', '30/09/2012 23:54', '30/09/2012 23:55',
'30/09/2012 23:56', '30/09/2012 23:57', '30/09/2012 23:58',
'30/09/2012 23:59'],
dtype='object', name='Date', length=491589)
在本例中,csv 文件如下所示:(最初它是一个文本文件,我将其重新保存为 CSV):
TimeW_1min,CONC_1min
01/04/2012 00:00,17.9
01/04/2012 00:01,-1.2
01/04/2012 00:02,16.8
同时,如果我使用原始 txt 文件:我得到:
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'
此时:df1.index 看起来像:
Index([], dtype='object', name='Date')
<小时/>
但是当我使用另一个看起来像这样的数据集时:
01-mar-05 12:00, 22.7, 8.1, 0.0214, 1.3727, 0.0214, 1.6969, 1.00,30.603
01-mar-05 12:05, -11.7, 8.1, 0.0214, 1.3725, 0.0214, 1.6965, 1.00,30.5871
它不仅运行程序,df1.index 看起来像:
DatetimeIndex(['2005-03-01 12:00:00', '2005-03-01 12:10:00',
'2005-03-01 12:15:00', '2005-03-01 12:20:00',
etc.
'2005-03-03 12:00:00'],
dtype='datetime64[ns]', name='Date', freq=None)
那么如何将第一个文件转换为 txt 或 csv 文件,以便以 datetime64[ns] 格式读取。
非常感谢
这里是原始文本文件的链接:我正在尝试让代码工作:
http://expirebox.com/download/fe01dc85c38e9bf13d477508006d7c94.html
但这给出了一种奇怪的格式: 所以我进入 Excel 并将其另存为 csv..,可以在这里找到:
http://expirebox.com/download/b984ecf365c4c19387a650eeb17f008f.html
第二个是我正在尝试使用的..但无济于事
将代码更改为:
aethalometer=['日期','浓度']
df1=pd.read_csv('BC_2012_1min.txt',names=aethalometer,parse_dates=True,skiprows=1,sep='\t').set_index('日期')
df1.index = df1.index.to_period('h')
现在打印出来为:
2012/9/30 23:58:00 12.40
2012/9/30 23:59:00 2.60
但是说:
AttributeError:'Index'对象没有属性'to_period
并且 df1.index 仍然是一个对象:
dtype='object', name='Date', length=491588)
尝试过:
df1.index = pd.to_datetime(df1.index)
但是这表示未知的字符串格式
最佳答案
好的,您的文件看起来像是通过您创建它的任何方法进行了 foo-ed,您的标题在行上重复:
43202、87843、132482、174243、186697、231338、274539、319180、363821、407022、448389
如下所示:
2012/4/30 23:59:00 -16.00
TimeW_1min CONC_1min
2012/8/1 00:00:00 15.10
因此,您可以做的是不要尝试解析日期列并使用 to_datetime
和参数 errors='coerce'
进行转换,这会将错误行转换为 <然后,您可以过滤出行并设置索引并根据需要转换为 PeriodIndex
:
In [126]:
df = pd.read_csv(r'c:\data\BC_2012_1min.txt', sep='\t', names=['Date','Conc'], skiprows=1 )
df['Date'] = pd.to_datetime(df['Date'], errors='coerce')
df = df[df['Date'].notnull()].set_index('Date').to_period('h')
df.index
Out[126]:
PeriodIndex(['2012-04-01 00:00', '2012-04-01 00:00', '2012-04-01 00:00',
'2012-04-01 00:00', '2012-04-01 00:00', '2012-04-01 00:00',
'2012-04-01 00:00', '2012-04-01 00:00', '2012-04-01 00:00',
'2012-04-01 00:00',
...
'2012-09-30 23:00', '2012-09-30 23:00', '2012-09-30 23:00',
'2012-09-30 23:00', '2012-09-30 23:00', '2012-09-30 23:00',
'2012-09-30 23:00', '2012-09-30 23:00', '2012-09-30 23:00',
'2012-09-30 23:00'],
dtype='int64', name='Date', length=491577, freq='H')
因此,在您的情况下,将我的第一行更改为:
aethalometer=['Date','Conc']
df1=pd.read_csv('BC_2012_1min.csv', names=aethalometer, sep='\t', skiprows=1)
关于python - 为什么 CSV 文件将第一列加载为 dtype64[ns],但将 txt 作为对象加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38049144/