如错误所示
Traceback (most recent call last):
File "E:fakepathpythonCSVmain.py", line 84, in <module>
print(to_date(df['start_time']))
File "E:fakepathpythonCSVmain.py", line 75, in to_date
return datetime.strftime(datetime.fromtimestamp(int(x)/1000).strftime("%d-%b-%Y"), "%d-%b-%Y")
File "C:Users%username%AppDataLocalProgramsPythonPython39libsite-packagespandascoreseries.py", line 141, in wrapper
raise TypeError(f"cannot convert the series to {converter}")
TypeError: cannot convert the series to <class 'int'>
我的代码是:
import pandas as pd
import glob
from datetime import datetime, timedelta
from pymongo import MongoClient
client = MongoClient()
col = client['right']['abcde']
listFileNames = (glob.glob(r"C:Users%username%DesktopBook1.csv"))
# print(len(listFileNames))
cols = ["start_time", "end_time", "source_Ip", "source_Mac", "destination_Ip", "destination_Mac"]
def get_merged_data_frame(list_file_names, p_index_col=False, p_header=None, columns=None):
if columns is None:
columns = cols
if len(list_file_names) == 1:
return pd.read_csv(list_file_names[0], index_col=p_index_col, header=p_header, low_memory=False,
names=columns,
usecols=[6, 7, 8, 9, 10, 11])
else:
df_from_each_file = (pd.read_csv(f, index_col=p_index_col, header=p_header, low_memory=False, names=columns,
usecols=[6, 7, 8, 9, 10, 11])
for f in list_file_names)
concatenated_df = pd.concat(df_from_each_file, ignore_index=True)
return concatenated_df
def to_date(x):
return datetime.strftime(datetime.fromtimestamp(int(x)/1000).strftime("%d-%b-%Y"), "%d-%b-%Y")
df = get_merged_data_frame(listFileNames)
print(df)
df['start_data'] = df['start_time'].apply(to_date)
print(to_date(df['start_time']))
print(type(df))
print(df)
data = df.to_dict(orient='records')
print(data)
col.insert_many(data)
我试过很多解决方案,我试过了,但我不知道我把它放在哪里了,对吗?许多解决方案称使用.astype(int)
或.astype(float)
df['start_data'] = df['start_time'].astype(int)
这个df['start_time']
看起来像这个
0 1617213592022005000
1 1617213592064079000
Name: start_time, Length: 3960, dtype: int64
但这似乎是错误的。。。
谢谢你的回答。。。
您可以尝试字符串表示(如函数to_date
(
df['start_data'] = pd.to_datetime(df['start_time'], unit='ns')
.dt.strftime("%d-%b-%Y")
>>> df
start_time start_data
0 1617213592022005000 31-Mar-2021
1 1617213592064079000 31-Mar-2021