Python无法比较日期框架中的日期

时间:2018-07-29 11:39:02

标签: python pandas datetime

我有一个数据框df ['最新日期']如下

InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS

我想过滤日期为> 22/07/2018的行

我尝试了String strInputType; final int inputType = editText.getInputType(); switch (inputType) { case (InputType.TYPE_TEXT_FLAG_CAP_WORDS|InputType.TYPE_CLASS_TEXT): { strInputType = "Name "; } break; case (InputType.TYPE_TEXT_VARIATION_PASSWORD|InputType.TYPE_CLASS_TEXT): { strInputType = "Password or Confirm Password "; } break; case (InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS|InputType.TYPE_CLASS_TEXT): { strInputType = "Email "; } break; case InputType.TYPE_CLASS_PHONE: { strInputType = "Phone Number "; } break; case InputType.TYPE_CLASS_DATETIME: { strInputType = "Date "; } break; case InputType.TYPE_CLASS_NUMBER: { strInputType = "Number "; } break; case InputType.TYPE_TEXT_VARIATION_POSTAL_ADDRESS: { strInputType = "Address "; } break; default: { strInputType = "Field "; } break; } Resources res = baseActivity.getResources(); String message = res.getString(R.string.field_blank, strInputType);

但是,新df不一定在2018年7月22日之后返回条目。我想知道是否不理解日期为“ dd / mm / yy”格式。

1 个答案:

答案 0 :(得分:2)

您首先需要使用参数public class FabButtonRenderer: Xamarin.Forms.Platform.Android.AppCompat.ViewRenderer<FabButton,FloatingActionButton> { public static void InitRenderer() { } public FabButtonRenderer(Context context):base(context) { } protected override void OnElementChanged(ElementChangedEventArgs<FabButton> e) { base.OnElementChanged(e); if (e.NewElement == null) return; if (e.NewElement.HeightRequest <= 0) e.NewElement.HeightRequest = 85; if (e.NewElement.WidthRequest <= 0) e.NewElement.WidthRequest = 75; if (e.NewElement.Margin.Equals(new Thickness(0, 0, 0, 0))) e.NewElement.Margin = new Thickness(0, 0, 5, 10); var fabButton = new FloatingActionButton(Context); ViewCompat.SetBackgroundTintList(fabButton, ColorStateList.ValueOf(Element.ButtonColor.ToAndroid())); fabButton.UseCompatPadding = true; if (!string.IsNullOrEmpty(Element.Image?.File)) fabButton.SetImageDrawable(Context.GetDrawable(Element.Image.File)); fabButton.Click += FabButton_Clicked; SetNativeControl(fabButton); } protected override void OnLayout(bool changed, int l, int t, int r, int b) { base.OnLayout(changed, l, t, r, b); Control.BringToFront(); } protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == nameof(Element.ButtonColor)) ViewCompat.SetBackgroundTintList(Control, ColorStateList.ValueOf(Element.ButtonColor.ToAndroid())); if (e.PropertyName == nameof(Element.Image)) { if (!string.IsNullOrEmpty(Element.Image?.File)) Control.SetImageDrawable(Context.GetDrawable(Element.Image.File)); } base.OnElementPropertyChanged(sender, e); } public void FabButton_Clicked(object sender, EventArgs e) { Element.SendClicked(); } } to_datetimedatetimes解析日期,http://strftime.org/,如果格式不同,另请参见What it's supposed to look like

format

df["Latest date"] = pd.to_datetime(df["Latest date"], format='%d.%m.%Y - %H:%M:%S')
#slowier solution without defined format
#df["Latest date"] = pd.to_datetime(df["Latest date"])

print (df["Latest date"].dtype)
datetime64[ns]

现在可以进行过滤了:

[30000 rows x 1 columns]
df = pd.concat([df] * 1000, ignore_index=True)

In [204]: %timeit df["Latest date1"] = pd.to_datetime(df["Latest date"], format='%d.%m.%Y - %H:%M:%S')
163 ms ± 3.2 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [205]: %timeit df["Latest date2"] = pd.to_datetime(df["Latest date"])
5.09 s ± 55.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

编辑:

如果输入数据位于df=df[(df["Latest date"]>"22/07/2018")] print (df) Latest date 4357 2018-07-24 16:00:36 4369 2018-07-23 09:13:34 4371 2018-07-23 09:14:13 4372 2018-07-23 09:14:44 4374 2018-07-23 09:15:35 4378 2018-07-23 09:15:58 4379 2018-07-23 09:16:24 4380 2018-07-23 09:16:41 4381 2018-07-23 09:17:16 4383 2018-07-23 09:17:53 4387 2018-07-23 09:18:28 4389 2018-07-23 09:19:25 4393 2018-07-23 09:20:08 4394 2018-07-25 14:25:20 4401 2018-07-23 09:21:16 4417 2018-07-24 11:56:14 4418 2018-07-25 11:05:11 中,则另一种可能的解决方案是按列位置或列名使用csv参数:

parse_dates