怎么把int64转换成分钟?蟒蛇熊猫

时间:2020-10-31 18:34:25

标签: python pandas datetime

我想将此列(int64格式)转换为分钟格式以进行计算

Duration(min)
10
30
5
15
5

我尝试过:

df['Duration(min)'] = pd.to_datetime(df['Duration(min)'], format='%H%M')

但是我有 ValueError::时间数据“ 5”与格式“%H%M”(匹配)不匹配

3 个答案:

答案 0 :(得分:2)

您需要在此处使用pd.to_timedelta

pd.to_timedelta(df['Duration(min)'], unit='min')

0   0 days 00:10:00
1   0 days 00:30:00
2   0 days 00:05:00
3   0 days 00:15:00
4   0 days 00:05:00
Name: Duration(min), dtype: timedelta64[ns]

unit-> ‘m’ / ‘minute’ / ‘min’ / ‘minutes’ / ‘T’都是分钟的简写。

答案 1 :(得分:2)

pd.to_datetimestrftime结合使用-

import SwiftUI

struct ContentView: View {
    @Environment(\.managedObjectContext) var moc
    @FetchRequest(entity: Task.entity(), sortDescriptors: []) var tasks: FetchedResults<Task>
    
    @State var isPresented = false
    var body: some View {
        VStack {
            NavigationView {
                ZStack {
                    List{
                        ForEach(tasks, id: \.id){ task in
                            CellView(completionState: task.completionState, title: task.title!)
                        }
                    }
                    
                    Button(action: {
                        self.isPresented.toggle()
                        print(isPresented)
                    }) {
                        CircleView()
                    }.offset(x: 158, y: 250)
                    
                }
                .navigationBarTitle("Infinito")
                .sheet(isPresented: $isPresented, content: {
                    EditView()
                        .environment(\.managedObjectContext, self.moc)
                })
            }
            HStack(alignment: .center, spacing: 97) {
                Button(action: {}) {
                    Image(systemName: "highlighter")
                        .resizable()
                        .frame(width: 45, height: 45)
                        .padding()
                        .foregroundColor(.orange)
                }.offset(x: 10)
                Button(action: {}){
                    Image(systemName: "timelapse")
                        .resizable()
                        .frame(width: 45, height: 45)
                        .offset(x: 4, y: 0)
                        .padding()
                        .foregroundColor(.green)
                }.offset(x: -7)
                .padding(.trailing, 20.0)
                Button(action: {}){
                    Image(systemName: "alarm")
                        .resizable()
                        .frame(width: 45, height: 45)
                        .foregroundColor(.red)
                }.offset(x: -24)
            }
        }
        
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
            return ContentView().environment(\.managedObjectContext, context)
        }
    }
}

struct CellView: View {
    @Environment(\.managedObjectContext) var moc
    @FetchRequest(entity: Task.entity(), sortDescriptors: []) var tasks: FetchedResults<Task>
    
    var completionState: Bool
    var title: String
    
    var body: some View {
        HStack{
            switch completionState{
            case false:
                Image(systemName: "square")
                
            case true:
                Image(systemName: "checkmark.square")
                    .foregroundColor(.green)
                
                Text(title)
                    .foregroundColor(.black)
                
            }
        }
    }
}

输出-

//
//  EditView.swift
//  Infinito
//
//  Created by Armando Visini on 28/10/2020.
//
import SwiftUI

struct EditView: View{
    @Environment(\.managedObjectContext) var moc
    @FetchRequest(entity: Task.entity(), sortDescriptors: []) var tasks: FetchedResults<Task>
    
    @State var titleOfTask: String = ""
   
    var body: some View {
        NavigationView {
            VStack(alignment: .leading){
                Text("Title of task")
                    .font(.title)
                    .fontWeight(.bold)
                HStack {
                    TextField("Enter name of task here...", text: $titleOfTask)
                        .textFieldStyle(RoundedBorderTextFieldStyle())
                    
                    Button(action: {
                        let task = Task(context: self.moc)
                        
                        task.id = UUID()
                        task.title = titleOfTask
                        task.completionState = false
                        
                        try? self.moc.save()
                        
                        UIApplication.shared.endEditing()
                    }) {
                        Text("Confirm")
                            .foregroundColor(.white)
                            .fontWeight(.medium)
                            .background(Color(.blue))
                            .cornerRadius(6.0)
                    }
                }

                
            }
            .padding()
            .offset(y: -200.0)
        }
    }
    
    struct EditView_Previews: PreviewProvider {
        static var previews: some View {
            EditView()
        }
    }
}


extension UIApplication{
    func endEditing() {
            sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
        }
}
//
//  EditView.swift
//  Infinito
//
//  Created by Armando Visini on 28/10/2020.
//
import SwiftUI

struct EditView: View{
    @Environment(\.managedObjectContext) var moc
    @FetchRequest(entity: Task.entity(), sortDescriptors: []) var tasks: FetchedResults<Task>
    
    @State var titleOfTask: String = ""
   
    var body: some View {
        NavigationView {
            VStack(alignment: .leading){
                Text("Title of task")
                    .font(.title)
                    .fontWeight(.bold)
                HStack {
                    TextField("Enter name of task here...", text: $titleOfTask)
                        .textFieldStyle(RoundedBorderTextFieldStyle())
                    
                    Button(action: {
                        let task = Task(context: self.moc)
                        
                        task.id = UUID()
                        task.title = titleOfTask
                        task.completionState = false
                        
                        try? self.moc.save()
                        
                        UIApplication.shared.endEditing()
                    }) {
                        Text("Confirm")
                            .foregroundColor(.white)
                            .fontWeight(.medium)
                            .background(Color(.blue))
                            .cornerRadius(6.0)
                    }
                }

                
            }
            .padding()
            .offset(y: -200.0)
        }
    }
    
    struct EditView_Previews: PreviewProvider {
        static var previews: some View {
            EditView()
        }
    }
}


extension UIApplication{
    func endEditing() {
            sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
        }
}

答案 2 :(得分:0)

为什么只有几分钟是%H%M?尝试format ='%M'。