【发布时间】:2020-01-26 12:26:15
【问题描述】:
我正在处理一个问题陈述,我必须代表每小时发生的错误数据传输。我指的是https://www.kaggle.com/gpsaikrishna/credit-card-fraud-detection-smote-deep-learning 仅用于数据表示目的。
我试图将数据放在我的工作目录中。仍然收到操作系统错误。我的系统的日期和时间在禁用夏令时自动更新。并格式化为 时间:24小时; HH:毫米和 日期:DD-MM-YYYY
下面是我尝试在 Kaggle 内核的系统上运行的代码
def convert_totime(seconds):
return datetime.datetime.fromtimestamp(seconds);
timeAnalysis = data[['Time', 'Amount', 'Class']].copy()
timeAnalysis['datetime'] = timeAnalysis.Time.apply(convert_totime)
timeDelta = datetime.datetime.utcnow() - datetime.datetime.now()
# As the max time is 172792 seconds and 172792 / (60*60) is about 48 hrs so we only have data for 2 days so only
# plotting data against hours make sense
timeAnalysis['hour of the day'] = timeAnalysis.datetime + timeDelta
timeAnalysis['hour of the day'] = timeAnalysis['hour of the day'].dt.hour
timeAnalysisGrouped = timeAnalysis.groupby(['Class', 'hour of the day'])['Amount'].count()
我收到一个错误
OSError Traceback (most recent call last)
<ipython-input-77-002a1f9a93fc> in <module>()
3
4 timeAnalysis = data[['Time', 'Amount', 'Class']].copy()
----> 5 timeAnalysis['datetime'] = timeAnalysis.Time.apply(convert_totime)
6 timeDelta = datetime.datetime.utcnow() - datetime.datetime.now()
7 # As the max time is 172792 seconds and 172792 / (60*60) is about 48 hrs so we only have data for 2 days so only
~\AppData\Local\conda\conda\envs\env\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwds)
3190 else:
3191 values = self.astype(object).values
-> 3192 mapped = lib.map_infer(values, f, convert=convert_dtype)
3193
3194 if len(mapped) and isinstance(mapped[0], Series):
pandas/_libs/src\inference.pyx in pandas._libs.lib.map_infer()
<ipython-input-77-002a1f9a93fc> in convert_totime(seconds)
1 def convert_totime(seconds):
----> 2 return datetime.datetime.fromtimestamp(seconds);
3
4 timeAnalysis = data[['Time', 'Amount', 'Class']].copy()
5 timeAnalysis['datetime'] = timeAnalysis.Time.apply(convert_totime)
OSError: [Errno 22] Invalid argument
我希望使用以下代码显示欺诈交易数量的图表:
plt.figure(figsize = (10, 6))
fraudTransactions = timeAnalysisGrouped[1].copy()
fraudTransactions.name = 'Number of transactions'
fraudTransactions.plot.bar(title = 'Number of fraud credit card transactions per hour', legend = True)
【问题讨论】:
-
参考此docs.python.org/3/library/… 并检查您是否正确使用了该功能。
-
是的,在
datetime的代码中正确调用了函数
标签: python-3.x windows pandas datetime operating-system