按小时舍入datetime列

时间:2018-01-08 05:30:25

标签: python pandas date datetime dataframe

        string status = Request.QueryString["status"];
        string apiRef = Request.QueryString["trans_no"];
        string refId = Request.QueryString["client_key"];
        string oRefId = Request.QueryString["success_id"];

        if (status == "success")
            status = "Success";

        SqlConnection conn = new SqlConnection(WebConfigurationManager.AppSettings["ConnString1"]);
        conn.Open();
        SqlCommand cmd = new SqlCommand("UPDATE mytable SET status=@status ,oreference_id= @oRefId ,udate=GETDATE() WHERE ureference_id= @refId AND areference_id= @apiRef", conn);

        cmd.Parameters.AddWithValue("@status", status);
        cmd.Parameters.AddWithValue("@oRefId", oRefId);
        cmd.Parameters.AddWithValue("@refId", refId);
        cmd.Parameters.AddWithValue("@apiRef", apiRef);

        cmd.ExecuteNonQuery();
        conn.Close();

假设我有上面的数据,我所有A栏的输出应该是:11/22/2017 10:00,综合功能不起作用,因为10:30之后的时间被认为是11:00,因此需要帮助忽略分钟和几秒钟准备好我的确切数据以供进一步分析。

3 个答案:

答案 0 :(得分:3)

从 -

开始
 CREATE OR REPLACE FUNCTION public.get_available_product_details(
    IN start_day_id integer,
    IN end_day_id integer)
     RETURNS SETOF record AS
 $BODY$declare
 begin    
    SELECT pd.days_id As pd_days_id, pd.id AS p_id, pd.name AS p_name
   FROM  product p JOIN product_days pd   
     Using(id)
     WHERE  pd.id in 
     Select * from 
      //here I need to use the result of the getall_available_products 
      //function
  end;         
 $BODY$
 LANGUAGE plpgsql VOLATILE

首先,使用print(s) 11/22/2017 10:00 14.442473 11/22/2017 10:05 19.446146 11/22/2017 10:10 49.382300 11/22/2017 10:15 51.216980 11/22/2017 10:20 50.674092 11/22/2017 10:25 14.893244 11/22/2017 10:30 27.191617 11/22/2017 10:35 19.826802 11/22/2017 10:40 9.996578 11/22/2017 10:45 7.929272 11/22/2017 10:50 22.770500 11/22/2017 10:55 32.611105 Name: Data, dtype: float64 -

将索引转换为日期时间索引
pd.to_datetime

假设日期是此df.index = pd.to_datetime(df.index, errors='coerce') 索引的一部分,请使用Series函数以及每小时频率作为日期时间 -

floor

如果您要在数据框列(例如s.index = s.index.floor('H') print(s) 2017-11-22 10:00:00 14.442473 2017-11-22 10:00:00 19.446146 2017-11-22 10:00:00 49.382300 2017-11-22 10:00:00 51.216980 2017-11-22 10:00:00 50.674092 2017-11-22 10:00:00 14.893244 2017-11-22 10:00:00 27.191617 2017-11-22 10:00:00 19.826802 2017-11-22 10:00:00 9.996578 2017-11-22 10:00:00 7.929272 2017-11-22 10:00:00 22.770500 2017-11-22 10:00:00 32.611105 Name: Data, dtype: float64 )上应用floor功能,请使用Date访问者 -

.dt

答案 1 :(得分:2)

如@cᴏʟᴅsᴘᴇᴇᴅ所述,您需要解决转换为datetime

保持熊猫。我坚持使用@cᴏʟᴅsᴘᴇᴇᴅ的答案,但是这样说:

df.assign(Date=pd.to_datetime(df.Date).dt.floor('H'))

                  Date          A
0  2017-11-22 10:00:00  14.442473
1  2017-11-22 10:00:00  19.446146
2  2017-11-22 10:00:00  49.382300
3  2017-11-22 10:00:00  51.216980
4  2017-11-22 10:00:00  50.674092
5  2017-11-22 10:00:00  14.893244
6  2017-11-22 10:00:00  27.191617
7  2017-11-22 10:00:00  19.826802
8  2017-11-22 10:00:00   9.996578
9  2017-11-22 10:00:00   7.929272
10 2017-11-22 10:00:00  22.770500
11 2017-11-22 10:00:00  32.611105

但是使用Numpy类型的替代方案

df.assign(Date=pd.to_datetime(df.Date).values.astype('datetime64[h]'))

                  Date          A
0  2017-11-22 10:00:00  14.442473
1  2017-11-22 10:00:00  19.446146
2  2017-11-22 10:00:00  49.382300
3  2017-11-22 10:00:00  51.216980
4  2017-11-22 10:00:00  50.674092
5  2017-11-22 10:00:00  14.893244
6  2017-11-22 10:00:00  27.191617
7  2017-11-22 10:00:00  19.826802
8  2017-11-22 10:00:00   9.996578
9  2017-11-22 10:00:00   7.929272
10 2017-11-22 10:00:00  22.770500
11 2017-11-22 10:00:00  32.611105

答案 2 :(得分:0)

好吧,谢谢大家的回答,这是我尝试过的,也是有效的,希望它可能对某人有帮助,因此张贴..

我试过了:

-std=c++98