【问题标题】:How to fix 'OSError: [Errno 22] invalid argument' datetime Windows 10如何修复“OSError:[Errno 22] 无效参数”日期时间 Windows 10
【发布时间】: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)

【问题讨论】:

标签: python-3.x windows pandas datetime operating-system


【解决方案1】:

您是否已经尝试用.map 代替.apply。 Apply 将 Series 对象传递给函数参数,而不是元素本身。因此,换句话说,您通常在使用“矢量化”函数时使用它。

您的函数适用于单个向量,而不适用于系列,这就是您的代码的问题。

所以你可以试试:

timeAnalysis.Time.map(convert_totime)

这应该可以解决您的主要问题。此外,您还可以检查是否可以直接使用更多本机 pandas 数据类型。进行类型转换的最方便的方法。如果您还没有尝试过,您可以使用内置的numpy 逻辑检查您的日期时间值是否正确解析,如下所示:

timeAnalysis.Time.astype('datetime64')

或者,您也可以像这样在函数中使用pandas.to_datetime

def convert_totime(series):
    return pd.to_datetime(series)

如果您要转换的值是整数,应该被解释为秒,您需要将unit='s' 传递给to_datetime。 如果值以字符串格式显示,您可能需要将format= 与适当的格式字符串一起使用。有关该功能的更多信息,请参见此处:Description of pandas.to_datetime

【讨论】:

  • 正如你所说,我将 .apply 替换为 .map 。没有任何更改仍然出现相同的错误。
  • 那我觉得你这个栏目的格式不支持。请检查上面的代码,如果它解决了问题。
  • timeAnalysis.Time.astype('datetime64') 数据长度为 284807 所有值都包含相同的纪元 1970-01-01 只是增加了纳秒。
  • 我用你的建议替换了def convert_totime(seconds): return datetime.datetime.fromtimestamp(seconds);。将.apply 替换为.map。我只是在时间间隔18 得到一根柱子
  • 嗯,如果你能提供一些测试数据可能会有所帮助。否则只是猜测。
【解决方案2】:
def convert_totime(series):
    return pd.to_datetime(series, unit='s')

timeAnalysis.Time.apply(convert_totime)

【讨论】:

  • 请解释你的答案。
猜你喜欢
  • 1970-01-01
  • 2021-10-13
  • 2022-10-18
  • 2021-11-01
  • 2019-04-27
  • 1970-01-01
  • 2018-10-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多