在Azure Python中的两个日期列之间查找日期

时间:2018-06-20 17:16:42

标签: python datetime azure-machine-learning-studio strptime strftime

我正在尝试查找一列中所有其他两列之间的日期,但是我仍然从限制之外获取结果。我怀疑这可能是strftime的问题,我应该改用strptime,但似乎无法正常工作。

我的数据是这样的:

Prod_Date                 Date                      Lead_Date
08/02/1985 12:00:00 AM    08/02/1970 12:00:00 AM    08/02/1988 12:00:00 AM
08/02/1986 12:00:00 AM    08/02/1971 12:00:00 AM    08/02/2018 12:00:00 AM
08/02/1987 12:00:00 AM    08/02/1972 12:00:00 AM    08/02/1986 12:00:00 AM
08/02/1988 12:00:00 AM    08/02/1973 12:00:00 AM    08/02/2018 12:00:00 AM

我想将数据框限制为介于Date和Lead_Date列之间的Prod_Date值。我的代码没有任何错误,但是我会在Date值之前或Lead_Date值之后得到很多错误的值。

# imports up here can be used to 
import pandas as pd
import datetime

# The entry point function can contain up to two input arguments:
#   Param<dataframe1>: a pandas.DataFrame
#   Param<dataframe2>: a pandas.DataFrame
def azureml_main(dataframe1 = None, dataframe2 = None):

    # Convert strings to datetime
    dataframe1['Date'] = dataframe1.Date.apply(
        lambda x: pd.to_datetime(x).strftime('%d/%m/%Y %I:%M:%S %p'))

    dataframe1['Prod_Date'] = dataframe1.Prod_Date.apply(
        lambda x: pd.to_datetime(x).strftime('%d/%m/%Y %I:%M:%S %p'))
 
    dataframe1['Lead_Date'] = dataframe1.Lead_Date.apply(
        lambda x: pd.to_datetime(x).strftime('%d/%m/%Y %I:%M:%S %p'))
        
    # Keeping only the rows for production that are contained between the date and lead_date:
    dataframe1 = dataframe1[(dataframe1['Date'] < dataframe1['Prod_Date']) & (dataframe1['Prod_Date'] < dataframe1['Lead_Date'])]

0 个答案:

没有答案