Python 相当于月日期时间的sql

Python 相当于月日期时间的sql,python,pandas,date,Python,Pandas,Date,我有一个熊猫数据框架,我需要像特定月份的sql查询一样进行过滤。每次运行代码时,我都希望它从上个月获取数据,而不管当前月份的具体日期是什么 我的SQL代码在这里,但我需要它 其中DATEPART(m,logged)=DATEPART(m,DATEADD(m,-1,getdate()) 基于问题编辑进行修改: df = pd.DataFrame({'month': ['1-05-01 00:00:00','1-06-01 00:00:00','1-06-01 00:00:00','1-05-01

我有一个熊猫数据框架,我需要像特定月份的sql查询一样进行过滤。每次运行代码时,我都希望它从上个月获取数据,而不管当前月份的具体日期是什么

我的SQL代码在这里,但我需要它

其中DATEPART(m,logged)=DATEPART(m,DATEADD(m,-1,getdate())


基于问题编辑进行修改:

df = pd.DataFrame({'month': ['1-05-01 00:00:00','1-06-01 00:00:00','1-06-01 00:00:00','1-05-01 00:00:00']})
df['month'] = pd.to_datetime(df['month'])  

## To get it to the right format
import datetime as dt
df['month'] = df['month'].apply(lambda x: dt.datetime.strftime(x, '%Y-%d-%m'))
df['month'] = pd.to_datetime(df['month'])  

## Extract the month from this date
df['month_ex'] = df.month.dt.month

## Get current month to get the latest month from the dataframe, which is the previous month of the current month
from datetime import datetime
currentMonth = datetime.now().month

newDf = df[df.month_ex == currentMonth - 1]
输出:

       month  month_ex
1 2001-06-01         6
2 2001-06-01         6

请提供相应的答案和您的问题。即时功能。如果您尝试了一些方法并用您的尝试更新了您的问题,那么我们将更好地帮助您如何在堆栈溢出问题中显示pandas数据框?这其中的哪一部分将仅基于当前月份提取上个月的数据?这就是我所需要的。你所说的上个月的数据是什么意思?第二行是当前日期-1个月,第四行是前一个月的第一个日期。如果我现在在7月份运行此代码,那么什么会只提供6月份的数据?我使用的数据集只有两个月。例如,5月和6月,我只想要时间戳为6月的数据。“如果我现在在7月运行此代码,什么会只给我6月的数据?”-要解释您真正想要的数据越来越困难。请添加示例输入和输出示例。完美!谢谢你的帮助!
       month  month_ex
1 2001-06-01         6
2 2001-06-01         6