在dataframe中减去同一列的2个日期时间

时间:2017-09-02 15:52:54

标签: python dataframe

我有以下数据框,想要执行以下逻辑。

如果案例中的第二行与第一行相同,则进行编辑减法(时间戳)并将其作为整数(天数)放在单独的列中

package com.aztechdev.squad.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.aztechdev.squad.helpers.common;
import com.aztechdev.squad.model.DirectChatModel;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import hani.momanii.supernova_emoji_library.Helper.EmojiconTextView;

import com.aztechdev.squad.R;


public class DirectFirebaseAdapter extends FirebaseRecyclerAdapter<DirectChatModel,DirectFirebaseAdapter.MyChatViewHolder> {

private static final int RIGHT_MSG = 0;
private static final int LEFT_MSG = 1;
private static final int RIGHT_MSG_IMG = 2;
private static final int LEFT_MSG_IMG = 3;

public static final String TAG = "DirectFirebaseAdapter";
private ClickListenerChatFirebase mClickListenerChatFirebase;

private String mUID;
private Context mContext;

public DirectFirebaseAdapter(Context context, DatabaseReference ref, String mUID, ClickListenerChatFirebase mClickListenerChatFirebase) {
    super(DirectChatModel.class, R.layout.item_message_left, DirectFirebaseAdapter.MyChatViewHolder.class, ref);
    this.mUID = mUID;
    this.mClickListenerChatFirebase = mClickListenerChatFirebase;
    this.mContext = context;

}


@Override
public MyChatViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;
    if (viewType == RIGHT_MSG){
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message_right,parent,false);
        return new MyChatViewHolder(view);
    }else if (viewType == LEFT_MSG){
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message_left,parent,false);
        return new MyChatViewHolder(view);
    }else if (viewType == RIGHT_MSG_IMG){
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message_right_img,parent,false);
        return new MyChatViewHolder(view);
    } else if (viewType == 10) {
        return null;
    } else {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message_left_img,parent,false);
        return new MyChatViewHolder(view);
    }
}


@Override
public int getItemViewType(int position) {

    DirectChatModel model = getItem(position);

    Log.d(TAG, "getItemCount: " + getItemCount());
    Log.d(TAG, "From UID: " + model.getFrom().getId());
    Log.d(TAG, "To UID: " + model.getTo().getId());


    **if (!model.getFrom().getId().equals(mUID) && !model.getTo().getId().equals(mUID)) {
        Log.d(TAG, "getItemViewType: UID not found");
        return 10;
    }**


    if (model.getMapModel() != null){
        if (model.getFrom().getId().equals(mUID)){
            return RIGHT_MSG_IMG;
        }else{
            return LEFT_MSG_IMG;
        }
    }else if (model.getFile() != null){
        if (model.getFile().getType().equals("img") && model.getFrom().getId().equals(mUID)){
            return RIGHT_MSG_IMG;
        }else{
            return LEFT_MSG_IMG;
        }
    }else if (model.getFrom().getId().equals(mUID)){
        return RIGHT_MSG;
    }else {
        return LEFT_MSG;
    }
}

@Override
protected void populateViewHolder(MyChatViewHolder viewHolder, DirectChatModel model, int position) {

    if (model.getTo().getId().equals(mUID)) {
        viewHolder.setIvUser(model.getTo().getPhoto_profile());
        viewHolder.setTvTimestamp(model.getTimeStamp(), model.getTo().getName());

    } else {
        viewHolder.setIvUser(model.getFrom().getPhoto_profile());
        viewHolder.setTvTimestamp(model.getTimeStamp(), model.getFrom().getName());

    }

    viewHolder.setTxtMessage(model.getMessage());
    viewHolder.tvIsLocation(View.GONE);
    if (model.getFile() != null){
        viewHolder.tvIsLocation(View.GONE);
        viewHolder.setIvChatPhoto(model.getFile().getUrl_file());
    }else if(model.getMapModel() != null){
        viewHolder.setIvChatPhoto(common.local(model.getMapModel().getLatitude(),model.getMapModel().getLongitude()));
        viewHolder.tvIsLocation(View.VISIBLE);
    }
}


public class MyChatViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView tvTimestamp,tvLocation;
    EmojiconTextView txtMessage;
    ImageView ivUser,ivChatPhoto;

    public MyChatViewHolder(View itemView) {
        super(itemView);
        tvTimestamp = (TextView)itemView.findViewById(R.id.timestamp);
        txtMessage = (EmojiconTextView)itemView.findViewById(R.id.txtMessage);
        tvLocation = (TextView)itemView.findViewById(R.id.tvLocation);
        ivChatPhoto = (ImageView)itemView.findViewById(R.id.img_chat);
        ivUser = (ImageView)itemView.findViewById(R.id.ivUserChat);
    }

    @Override
    public void onClick(View view) {
        int position = getAdapterPosition();
        DirectChatModel model = getItem(position);
        if (model.getMapModel() != null){
            mClickListenerChatFirebase.clickImageMapChat(view,position,model.getMapModel().getLatitude(),model.getMapModel().getLongitude());
        }else{
           // mClickListenerChatFirebase.clickImageChat(view,position,model.getUserModel().getName(),model.getUserModel().getPhoto_profile(),model.getFile().getUrl_file());
        }
    }

    public void setTxtMessage(String message){
        if (txtMessage == null)return;
        txtMessage.setText(message);
    }

    public void setIvUser(String urlPhotoUser){
        if (ivUser == null)return;
        CircleTransform circleTransform = new CircleTransform();
        GlideApp.with(ivUser.getContext()).load(urlPhotoUser).centerCrop().transform(circleTransform).override(160,160).into(ivUser);

    }

    public void setTvTimestamp(String timestamp, String sender){
        if (tvTimestamp == null)return;
        tvTimestamp.setText(sender + " - " + convertTimestamp(timestamp));
    }

    public void setIvChatPhoto(String url){
        if (ivChatPhoto == null)return;

        GlideApp.with(ivChatPhoto.getContext()).load(url)
                .override(200, 200)
                .fitCenter()
                .into(ivChatPhoto);
        ivChatPhoto.setOnClickListener(this);
    }

    public void tvIsLocation(int visible){
        if (tvLocation == null)return;
        tvLocation.setVisibility(visible);
    }

}

private CharSequence convertTimestamp(String millis){
    return DateUtils.getRelativeTimeSpanString(Long.parseLong(millis), System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);
}

}

对此的任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

使用pandas并尝试使用您的数据框df :(改编自this question):

result = df.groupby('case')['edit'].agg(['max','min'])
result['diff'] = result['max']-result['min']
print(result[['diff']])