如何在python中将日期的日期编号设置为1-30中的任意随机数?

时间:2017-05-07 10:17:01

标签: python python-2.7 python-3.x date pandas

你好我有一个dateSting包含日期说2017-10

现在我将这个日期传递给了 dateparse = lambda dates: pandas.datetime.strptime(dates, '%Y-%m') 这个功能 并且它会将2017-10-01作为日期类型返回,但现在我希望在该日期有0-31而不是01的任意随机数我怎么能实现这个

在数组中如何交换所有行的列数据?

由于

1 个答案:

答案 0 :(得分:0)

我这样做:

package com.carxsing.anglelockscreen.GestureDetector;

public class GestureService extends Service implements 
  GestureOverlayView.OnGesturePerformedListener{

  private WindowManager windowManager;
  private GestureOverlayView GestureOverlay, GestureOverlay2;
  private GestureLibrary mGestureLib;

public GestureService() {
}

@Override public IBinder onBind(Intent intent) {
    return null;
}

@Override public void onCreate() {
    super.onCreate();
    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    GestureOverlay = new GestureOverlayView(this);
    GestureOverlay2 = new GestureOverlayView(this);
    GestureOverlay.setGestureColor(00000000);
    GestureOverlay.setUncertainGestureColor(00000000);
    GestureOverlay2.setGestureColor(00000000);
    GestureOverlay2.setUncertainGestureColor(00000000);
    GestureOverlay.addOnGesturePerformedListener(this);
    GestureOverlay2.addOnGesturePerformedListener(this);
    mGestureLib = GestureLibraries.fromRawResource(this, R.raw.gestures);
    if (!mGestureLib.load()) {
        mGestureLib.save();
    }
    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
            PixelFormat.TRANSLUCENT);
    params.height = 450;
    params.width = 75;
    params.gravity = Gravity.BOTTOM | Gravity.LEFT;
    windowManager.addView(GestureOverlay, params);
    params.height = 450;
    params.width = 75;
    params.gravity = Gravity.BOTTOM | Gravity.RIGHT;
    windowManager.addView(GestureOverlay2, params);

}

@Override
public void onDestroy() {
    super.onDestroy();
    if (GestureOverlay != null) windowManager.removeView(GestureOverlay);
    if (GestureOverlay2 != null) windowManager.removeView(GestureOverlay2);
}
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
    ArrayList<Prediction> predictions = mGestureLib.recognize(gesture);
    if (predictions.size() > 0) {
        Prediction prediction = predictions.get(0);
        if (prediction.score > 1.0) {
            if (prediction.name.equals(Gestures.bottomleft) || prediction.name.equals(Gestures.bottomright)) {
                Intent i = new Intent(GestureService.this, LockScreenActivity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.putExtra("start", 1);
                startActivity(i);
            }
        }
    }
}

更新: new feauture in Pandas 0.20.1:

  

In [21]: def random_dt(s): ...: d = pd.to_datetime(s, format='%Y-%m') ...: return d + pd.DateOffset(days=random.randint(0, d.daysinmonth-1)) ...: In [22]: random_dt('2017-10') Out[22]: Timestamp('2017-10-31 00:00:00') In [23]: random_dt('2017-10') Out[23]: Timestamp('2017-10-19 00:00:00') In [24]: random_dt('2017-10') Out[24]: Timestamp('2017-10-24 00:00:00') In [25]: random_dt('2017-10') Out[25]: Timestamp('2017-10-12 00:00:00') 获得了一个新参数to_datetime()来定义a   从何处计算生成的时间戳的参考日期   使用指定的特定单位解析数值。 (GH11276,   GH11745)

origin

演示:

def random_dt(s, periods=1, format='%Y-%m'):
    d = pd.to_datetime(s, format=format)
    return pd.to_datetime(np.random.randint(0, d.daysinmonth-1, periods), 
                          unit='D', origin=d)