通过字符串日期过滤熊猫的过滤数据

发布于 2025-01-30 01:11:26 字数 1584 浏览 2 评论 0原文

这样

看起来一个
数据
一些价值
2011-01-04一些价值
......
2012-01-02某些价值
2012-01-03某些值
2012-01-04某些值

日期列(%y-%m-%d)是dtype dateTime64 [ns]和参数列为dtype float64

我也有一个字符串变量'limit_date'

我想获得一个DF,该df只有与limit_date之前的日期相对应的行。

我已经使用了这两种不同的方法,他们起作用了: df [df ['date']<'2011-01-01'] 20110101')

在两种情况下,我都会得到像这样看起来像这样的东西:

日期参数
2010-01-02某些值
2010-01-03某些值
2010-01-04一些值
...df.Query('date&lt ;
2010-12-30有些价值
2010-12-31然而,一些价值

。如果我想使用字符串limit_date,我会遇到问题:

  • 当我使用limit_date ='2011-01-01'和df [df ['date'']< limt_date]时,我得到一个空的df

  • 当我使用limit_date ='2011-01-01'和df [df ['date']< limt_date]时,我会在使用limit_date ='1100101'和df.query('date&limit_date时,

    ')我有错误:

      ** undefinedVariable Error:名称'limit_date'未定义**
     

有人知道如何解决这个问题吗?

我需要在代码中使用字符串变量BC。此limit_date将在整个代码中更改。

I have a DataFrame that looks like this:

DateParameter
2010-01-02some value
2010-01-03some value
2010-01-04some value
......
2011-01-02some value
2011-01-03some value
2011-01-04some value
......
2012-01-02some value
2012-01-03some value
2012-01-04some value

Date column (%Y-%m-%d) is Dtype datetime64[ns] and Parameter column is Dtype float64

I also have a string variable 'limit_date'

I want to get a DF that has just the rows corresponding to the dates before limit_date.

I have used this two different approaches and they worked:
df[df['Date']<'2011-01-01']
df.query('Date<20110101')

I get something that looks like this in both cases:

DateParameter
2010-01-02some value
2010-01-03some value
2010-01-04some value
......
2010-12-30some value
2010-12-31some value

However. If I want to use the string limit_date I run into problems:

  • When I use limit_date = '2011-01-01' AND df[df['Date']<limit_date] I get an empty DF

  • When I use limit_date = '20110101' AND df.query('Date<limit_date') I get the error :

         **UndefinedVariableError: name 'limit_date' is not defined**
    

Does anyone have any idea how to deal with this issue?

I need to use a string variable bc in my code this limit_date will change throughout the code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

远昼 2025-02-06 01:11:26

dataframe.query期望您参考列名; df.query('date&lt; limit_date')正在尝试查询行,其中'date'date'列中的值小于'中的值limit_date'列。 (它如何知道'date'是列,但是limit_date是一个变量,鉴于您类似地引用它们?)

以区分列和环境变量,您将需要在环境变量之前添加@

df.query('Date < @limit_date')
#        Date
#0 2010-01-01
#1 2010-01-02
#2 2010-01-03
#3 2010-01-04

您的第一次尝试:df [df ['date']&lt; limit_date]在技术上是正确的,应该起作用。可能,您将以前的过滤器应用于数据框架并覆盖结果,因此现在,当您应用此过滤器时,它会导致空数据帧。刷新内核,从头开始。


示例数据

import pandas as pd

df = pd.DataFrame({'Date': pd.date_range('2010-01-01', freq='D', periods=20)})
limit_date = '2010-01-05'

DataFrame.query expects you to reference column names; df.query('Date < limit_date') is trying to query rows where the value in the 'Date' column is less than the value in the 'limit_date' column. (How would it know 'Date' is a column but limit_date is a variable, given that you refer to them similarly?)

To distinguish between columns and an environment variable you will need to add @ before the environment variables.

df.query('Date < @limit_date')
#        Date
#0 2010-01-01
#1 2010-01-02
#2 2010-01-03
#3 2010-01-04

Your first attempt: df[df['Date'] < limit_date] is technically correct and should work. Likely, you applied a previous filter to the DataFrame and overwrote the result, so that now when you apply this filter it results in an empty DataFrame. Refresh the kernel and start from the beginning.


Sample Data

import pandas as pd

df = pd.DataFrame({'Date': pd.date_range('2010-01-01', freq='D', periods=20)})
limit_date = '2010-01-05'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文