Python 循环浏览日期
我试图循环我创建的一些日期,但我得到了一个错误。代码如下:Python 循环浏览日期,python,pandas,loops,date,datetime,Python,Pandas,Loops,Date,Datetime,我试图循环我创建的一些日期,但我得到了一个错误。代码如下: q3_2018 = datetime.date(2018,9,30) q4_2018 = datetime.date(2018,12,31) q1_2019 = datetime.date(2019,3,31) q2_2019 = datetime.date(2018,6,30) dates = [q3_2018, q4_2018,q1_2019,q2_2019] values = [] for d in dates:
q3_2018 = datetime.date(2018,9,30)
q4_2018 = datetime.date(2018,12,31)
q1_2019 = datetime.date(2019,3,31)
q2_2019 = datetime.date(2018,6,30)
dates = [q3_2018, q4_2018,q1_2019,q2_2019]
values = []
for d in dates:
v = fin[fin['Date of Completion 1 payment']<d]['1st payment amount:\n(70%)'].sum()
values.append(v)
2018年第三季度=日期时间日期(2018年9月30日)
2018年第4季度=日期时间日期(2018年12月31日)
2019年第1季度=日期时间日期(2019,3,31)
2019年第2季度=日期时间日期(2018年6月30日)
日期=[2018年第三季度、2018年第四季度、2019年第一季度、2019年第二季度]
值=[]
对于d in日期:
v=fin[fin['dateofcompletion1payment']我建议将Date
s转换为datetimes
by,然后选择列使用:
或通过string
s进行比较:
dates = pd.to_datetime([q3_2018, q4_2018,q1_2019,q2_2019]).strftime('%Y-%m-%d')
print (dates)
Index(['2018-09-30', '2018-12-31', '2019-03-31', '2018-06-30'], dtype='object')
或:
什么是fin.dtypes
?第一次付款金额:\n(70%)float64完成日期1付款日期时间64[ns]最终付款:(30%)float64完成日期时间64[ns]不确定为什么返回axis=self.obj.\u get\u axis\u name(axis)))键错误:“标签[完成日期1付款]不在[索引]中,'而该列显然在索引中(我检查,没有空格问题,该列按原样拼写)…@filippoeBastio-通过print(df.columns.tolist())
检查列名,可能是一些编码或尾随空格问题。@filippoeBastio-哎呀,我这边的打字,.loc
首先需要fin.loc[fin][“完成日期1付款”]
dates = pd.to_datetime([q3_2018, q4_2018,q1_2019,q2_2019]).strftime('%Y-%m-%d')
print (dates)
Index(['2018-09-30', '2018-12-31', '2019-03-31', '2018-06-30'], dtype='object')
dates = ['2018-09-30', '2018-12-31' '2019-03-31','2018-06-30']
values = []
for d in dates:
v = fin.loc[fin['Date of Completion 1 payment']<d, '1st payment amount:\n(70%)'].sum()
values.append(v)
values = [fin.loc[fin['Date of Completion 1 payment']<d, '1st payment amount:\n(70%)'].sum()
for d in dates]
# 0.22.0... Silently coerce the datetime.date
>>> Series(pd.date_range('2017', periods=2)) == datetime.date(2017, 1, 1)
0 True
1 False
dtype: bool
# 0.23.0... Do not coerce the datetime.date
>>> Series(pd.date_range('2017', periods=2)) == datetime.date(2017, 1, 1)
0 False
1 False
dtype: bool
# 0.23.1... Coerce the datetime.date with a warning
>>> Series(pd.date_range('2017', periods=2)) == datetime.date(2017, 1, 1)
/bin/python:1: FutureWarning: Comparing Series of datetimes with 'datetime.date'. Currently, the
'datetime.date' is coerced to a datetime. In the future pandas will
not coerce, and the values not compare equal to the 'datetime.date'.
To retain the current behavior, convert the 'datetime.date' to a
datetime with 'pd.Timestamp'.
#!/bin/python3
0 True
1 False
dtype: bool