datetime struggle with a pandas dataframe

huangapple go评论111阅读模式
英文:

datetime struggle with a pandas dataframe

问题

以下是您提供的代码的翻译部分:

  1. 我正在运行以下 **代码**
  2. ```python
  3. filled_df = (ts.set_index(date_col)
  4. .groupby(grouping)
  5. .apply(lambda d: d.reindex(
  6. pd.date_range(
  7. mf_heuristic(d.index, champion_end), pd.to_datetime(
  8. dt.datetime.now()
  9. ),
  10. freq='MS')
  11. )
  12. )
  13. .drop(grouping, axis=1)
  14. .reset_index(grouping)
  15. .fillna(0)

使用以下预定义变量:

  1. date_col = 'timepoint'
  2. grouping = ['Level', 'Kontogruppe']
  3. champion_end = fc_date - pd.DateOffset(months=13)
  4. print(champion_end)
  5. 2021-03-01 00:00:00
  6. print(ts)
  7. Kontogruppe Level timepoint data_value
  8. 0 A ZBFO 2020-03-01 1
  9. 1 B ZBFO 2020-07-01 1612.59
  10. 2 A ZBFO 2020-08-01 1046.1
  11. print(ts.dtypes)
  12. Kontogruppe object
  13. Level object
  14. timepoint object
  15. data_value float64
  16. dtype: object
  1. def mf_heuristic(date, champion_end):
  2. if min(date) < champion_end:
  3. start_date = min(date)
  4. else:
  5. start_date = min(date)
  6. return start_date

当我运行代码时,我收到以下 错误消息

TypeError: Cannot compare Timestamp with datetime.date. Use ts == pd.Timestamp(date) or ts.date() == date instead.

针对具有 mf_heuristic 函数的行。

如果我尝试在之前加入以下代码:

  1. ts[date_col] = pd.Timestamp(ts[date_col])

我会收到以下错误消息:

TypeError: function missing required argument 'year' (pos 1)

  1. <details>
  2. <summary>英文:</summary>
  3. I am running the following **code**:

filled_df = (ts.set_index(date_col)
.groupby(grouping)
.apply(lambda d: d.reindex(
pd.date_range(
mf_heuristic(d.index, champion_end), pd.to_datetime(
dt.datetime.now()
),
freq='MS')
)
)
.drop(grouping, axis=1)
.reset_index(grouping)
.fillna(0)

  1. *with the predefinitions:*

date_col = 'timepoint'
grouping = ['Level', 'Kontogruppe']
champion_end = fc_date - pd.DateOffset(months=13)

print(champion_end)
2021-03-01 00:00:00

print(ts)
Kontogruppe Level timepoint data_value
0 A ZBFO 2020-03-01 1
1 B ZBFO 2020-07-01 1612.59
2 A ZBFO 2020-08-01 1046.1

print(ts.dtypes)
Kontogruppe object
Level object
timepoint object
data_value float64
dtype: object

def mf_heuristic(date, champion_end):

  1. if min(date) &lt; champion_end:
  2. start_date = min(date)
  3. else:
  4. start_date = min(date)
  5. return start_date
  1. When I run the code, I get the following **error massage**:
  2. &gt; TypeError: Cannot compare Timestamp with datetime.date. Use ts ==
  3. &gt; pd.Timestamp(date) or ts.date() == date instead.
  4. for the line with the mf_heuristic function.
  5. If I try to put before:

ts[date_col] = pd.Timestamp(ts[date_col])

  1. I get the error massage:
  2. &gt; TypeError: function missing required argument &#39;year&#39; (pos 1)
  3. </details>
  4. # 答案1
  5. **得分**: 2
  6. 尝试使用 `to_datetime`
  7. ```python
  8. ts[date_col] = pd.to_datetime(ts[date_col])
英文:

Try using to_datetime:

  1. ts[date_col] = pd.to_datetime(ts[date_col])

huangapple
  • 本文由 发表于 2023年7月27日 21:06:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780057.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定