卡在数据框查询上
Stuck on data frame query
我正在做一个项目,我在数据框中导入 SQLite 数据库,然后使用
将字符串日期转换为日期
toCorrect = ["dt_ocurred", "init_action_ship_dt","target_dt", "final_action_ship_dt", "done_dt",
"update_dt", "ext_dt", "PSC_picdt", "PSC_info2ownr_dt", "PSC_info2chrtr_dt", "PSC_info2rtshp_dt",
"PSC_info2oilmaj_dt", "PSC_info2mmstpmgmt_dt", "PSC_sndr_offimport_dt"]#
for someCol in toCorrect:
df_data[someCol] = pd.to_datetime(df_data[someCol],errors='coerce').apply(lambda x: x.date())
转换后,我正在尝试获取我有以下查询的当前年份的数据
curr_year = datetime.datetime.now().year
df_currDRS = df_rawData.query(
f"ship_name == '{shipName}' and (dt_ocurred.str.contains('{curr_year}') or done_dt.str.contains"
f"('{curr_year}') or status.str.contains('OPEN'))", engine='python')
由于某些我无法弄清楚的原因,我得到了这个错误
AttributeError: Can only use .str accessor with string values!
Traceback:
File "c:\users\sshukla\documents\drs2022\venv\lib\site-packages\streamlit\scriptrunner\script_runner.py", line 443, in _run_script
exec(code, module.__dict__)
File "C:\Users\sshukla\Documents\DRS2022\main.py", line 22, in <module>
make_NewDRS()
File "C:\Users\sshukla\Documents\DRS2022\GetNewDRS.py", line 39, in make_NewDRS
df_currDRS = df_rawData.query(
File "c:\users\sshukla\documents\drs2022\venv\lib\site-packages\pandas\core\frame.py",
我大概明白了,如果我不将字符串日期转换为日期对象,那么查询就可以正常运行。但是我想知道如果日期已经从字符串转换为日期,如何构造查询。
如有任何帮助,我们将不胜感激。
如果您想过滤给定年份(例如 2022 年)的日期列,您可以检查它是否介于该年的第一天和最后一天之间:
df.query(f"20220101 <= my_date <= 20221231")
应用于您的代码示例可能如下所示:
curr_year = datetime.datetime.now().year
df_currDRS = df_rawData.query(
f"ship_name == '{shipName}' and ("
f"({curr_year}0101 <= dt_ocurred <= {curr_year}1231) or "
f"({curr_year}0101 <= done_dt <= {curr_year}1231) or "
f"status.str.contains('OPEN'))", engine='python')
我正在做一个项目,我在数据框中导入 SQLite 数据库,然后使用
将字符串日期转换为日期toCorrect = ["dt_ocurred", "init_action_ship_dt","target_dt", "final_action_ship_dt", "done_dt",
"update_dt", "ext_dt", "PSC_picdt", "PSC_info2ownr_dt", "PSC_info2chrtr_dt", "PSC_info2rtshp_dt",
"PSC_info2oilmaj_dt", "PSC_info2mmstpmgmt_dt", "PSC_sndr_offimport_dt"]#
for someCol in toCorrect:
df_data[someCol] = pd.to_datetime(df_data[someCol],errors='coerce').apply(lambda x: x.date())
转换后,我正在尝试获取我有以下查询的当前年份的数据
curr_year = datetime.datetime.now().year
df_currDRS = df_rawData.query(
f"ship_name == '{shipName}' and (dt_ocurred.str.contains('{curr_year}') or done_dt.str.contains"
f"('{curr_year}') or status.str.contains('OPEN'))", engine='python')
由于某些我无法弄清楚的原因,我得到了这个错误
AttributeError: Can only use .str accessor with string values!
Traceback:
File "c:\users\sshukla\documents\drs2022\venv\lib\site-packages\streamlit\scriptrunner\script_runner.py", line 443, in _run_script
exec(code, module.__dict__)
File "C:\Users\sshukla\Documents\DRS2022\main.py", line 22, in <module>
make_NewDRS()
File "C:\Users\sshukla\Documents\DRS2022\GetNewDRS.py", line 39, in make_NewDRS
df_currDRS = df_rawData.query(
File "c:\users\sshukla\documents\drs2022\venv\lib\site-packages\pandas\core\frame.py",
我大概明白了,如果我不将字符串日期转换为日期对象,那么查询就可以正常运行。但是我想知道如果日期已经从字符串转换为日期,如何构造查询。
如有任何帮助,我们将不胜感激。
如果您想过滤给定年份(例如 2022 年)的日期列,您可以检查它是否介于该年的第一天和最后一天之间:
df.query(f"20220101 <= my_date <= 20221231")
应用于您的代码示例可能如下所示:
curr_year = datetime.datetime.now().year
df_currDRS = df_rawData.query(
f"ship_name == '{shipName}' and ("
f"({curr_year}0101 <= dt_ocurred <= {curr_year}1231) or "
f"({curr_year}0101 <= done_dt <= {curr_year}1231) or "
f"status.str.contains('OPEN'))", engine='python')