熊猫:转换时间系列YYYY-MM-DD hh:mm:ss.0仅保留YYYY-MM-DD格式

时间:2018-08-12 03:05:24

标签: python-3.x pandas time

熊猫:将时间序列YYYY-MM-DD hh:mm:ss.0转换为仅保留YYYY-MM-DD格式

python 3.6,pandas 0.19.0

     timestamp
0    2013-01-14 21:19:42.0
1    2013-01-16 09:04:37.0
2    2013-03-20 12:50:49.0
3    2013-01-03 17:02:53.0
4    2013-04-13 16:44:20.0

我尝试过:

df['timestamp'] = df['timestamp'].dt.strftime('%Y-%m-%d')

`AttributeError: Can only use .dt accessor with datetimelike values.`

有什么想法吗?谢谢!

3 个答案:

答案 0 :(得分:1)

它可能满足您的需求

initMap()

答案 1 :(得分:1)

将系列转换为日期时间数据类型,然后尝试

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {


    public static class PersonViewHolder extends RecyclerView.ViewHolder {
        CardView cv;
        TextView personName;
        TextView personAge;
        ImageView personPhoto;
        Button personbutton;

        PersonViewHolder(View itemView) {
            super(itemView);
            cv = (CardView)itemView.findViewById(R.id.cv);
            personName = (TextView)itemView.findViewById(R.id.person_name);
            personAge = (TextView)itemView.findViewById(R.id.person_age);
            personPhoto = (ImageView)itemView.findViewById(R.id.person_photo);
            personbutton=(Button)itemView.findViewById(R.id.imgbuttondownload);

        }
    }

    List<Person> persons;

    RVAdapter(List<Person> persons){
        this.persons = persons;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
        PersonViewHolder pvh = new PersonViewHolder(v);
        return pvh;
    }

    @Override
    public void onBindViewHolder(PersonViewHolder personViewHolder, final int i) {
        personViewHolder.personName.setText(persons.get(i).name);
        personViewHolder.personAge.setText(persons.get(i).age);
        personViewHolder.personPhoto.setImageResource(persons.get(i).photoId);
        personViewHolder.personPhoto.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                Context context=v.getContext();
                Intent intent=new Intent();
                switch (i){
                    case 0:
                        intent =  new Intent(context, MainActivity.class);
                        context.startActivity(intent);
                        break;
                    case 1:
                        intent =  new Intent(context, MainActivity.class);
                        context.startActivity(intent);
                        break;
                    case 2:
                        intent =  new Intent(context, MainActivity.class);
                        context.startActivity(intent);
                        break;
                    case 3:
                        intent =  new Intent(context, MainActivity.class);
                        context.startActivity(intent);
                        break;
                }
            }
        });

        personViewHolder.personbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (i){
                    case 0:
                String url = "http://www.androidtutorialpoint.com/wp-content/uploads/2016/09/Beauty.jpg";
                        DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
                request.setDescription("my title");
                request.setTitle("my description");

                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(
                        DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "file"+i+".mp4");
                        manager.enqueue(request);
                break;
                    case 1:
                        String url2 = "http://www.androidtutorialpoint.com/wp-content/uploads/2016/09/Beauty.jpg";
                        DownloadManager manager2 = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                        DownloadManager.Request request2 = new DownloadManager.Request(Uri.parse(url2));
                        request2.setDescription("my title");
                        request2.setTitle("my description");

                        request2.allowScanningByMediaScanner();
                        request2.setNotificationVisibility(
                                DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

                        request2.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "file"+i+".mp4");
                        manager2.enqueue(request2);
                        break;


            }
        }
    });
    }

    @Override
    public int getItemCount() {
        return persons.size();
    }
}

答案 2 :(得分:0)

使用下面显示的方法也有助于实现相同的目的。

df ['timestamp'] = pd.to_datetime(df ['timestamp'])。dt.date

您可以参考以下链接中提供的文档,以方便地进行日期时间处理: https://pandas.pydata.org/pandas-docs/stable/api.html#datetimelike-properties