我有下一个约会:
SELECT CASES_COMMENT
FROM (SELECT *
FROM (SELECT *
FROM CASES_TABLE CT JOIN CASES_PRODUCTS_TABLE CPT
ON CT.ID = CPT.CASE_ID
JOIN PRODUCTS_TABLE
ON PT.ID = CPT.PRODUCT_ID
WHERE PRODUCT_CODE = 'VoIP') as cond1
WHERE cond1.PRODUCT_CODE = 'PSTN') as cond2
WHERE cond2.PRODUCT_CODE = 'IPTV'
这是名为data的数据框的一部分:
0 2019-06-29
Name: end date, dtype: datetime64[ns]
然后我用下一个代码对其进行转换:
data
PointAlias start Date end date
0 AO122732 2018-06-29 2019-06-29
我想知道是否有可能将其从列表转换为仅字符串以应用end_date_transformed = []
for x in range(len(data['end date'])):
transf = pd.to_datetime(data['end date'][x]).date()
end_date_transformed.append(transf)
end_date_transformed
output: [datetime.date(2019, 6, 29)]
。
有办法吗?
这样做的最终目的是能够过滤日期的数据框以仅获取datetime.datetime.strftime(end_date_transformed, %Y%m%d')
的那些日期。像这样:
<= end_date_transformed
我知道上面的代码将不起作用,但是如果我执行datetime.date(year,month,day)并像df
date
0 2018-06-29
1 2018-10-29
2 2019-02-28
3 2019-06-29
4 2019-10-29
5 2020-02-29
df[df['date'] <= end_date_transformed] # This does not work, the output is:
ValueError: Arrays were different lengths: 44 vs 1
一样使用它,它将起作用,因此问题出在结束日期的转换中。
如果有人可以提供帮助,我将不胜感激。
非常感谢您抽出宝贵的时间阅读和回答。
答案 0 :(得分:1)
这应该在一个结束日期为您服务:
import pandas as pd
from datetime import datetime
data = pd.DataFrame([['A0122732','2018-06-29','2019-06-29']], columns=['PointAlias','start date','end date'])
df = pd.DataFrame([
['2018-06-29'],
['2018-10-29'],
['2019-02-28'],
['2019-06-29'],
['2019-10-29'],
['2020-02-29']],
columns=['date'])
df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d')
end_date = datetime.strptime(data['end date'].values[0], '%Y-%m-%d')
df[df['date'] <= end_date]
返回:
date
0 2018-06-29
1 2018-10-29
2 2019-02-28
3 2019-06-29