我有MS Excel格式的数据上传。 enter image description here
此文件的列中的日期为“ dd.mm.yyyy 00:00:00 ”格式。 使用代码读取文件:
private void signInWithPhoneAuthCredential(final PhoneAuthCredential credential) {
fbAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
textview2.setText("Verified");
if (credential != null) {
if (credential.getSmsCode() != null) {
codeText.setText(credential.getSmsCode());
} else {
codeText.setText(R.string.instant_validation);
//codeText.setTextColor(Color.parseColor("#4bacb8"));
}
}
//
signoutButton.setEnabled(true);
//codeText.setText(" ");
statusText.setText("Signed In");
resendButton.setEnabled(true); //false
verifyButton.setEnabled(true);
// verifyButton.setVisibility(View.GONE);
FirebaseUser user = task.getResult().getUser();
Log.d(TAG,"current user " +user);
String phoneNumber = user.getPhoneNumber();
UpdateUI(user);
} else {
if (task.getException() instanceof
FirebaseAuthInvalidCredentialsException) {
// The verification code entered was invalid
Log.d(TAG,"Invalid code");
selectPage3();
//resendButton.setEnabled(true);
//verifyButton.setEnabled(true);
}
}
}
});
}
我收到一个框架,其中日期列的类型为“ object ”。此外,我通过命令将此列转换为日期格式:
fetchFriendsList=(eventId)=>{
let allEventsList = [...this.state.allEventsList];
firebase.database().ref('/events').child(eventId).once('value').then((snapshot) => {
let data = snapshot.val();
let items = Object.values(data);
allEventsList.push(items);
this.setState({allEventsList : allEventsList});
ToastAndroid.show("kk>"+allEventsList.length , ToastAndroid.SHORT);
}).then((data)=>{
}).catch((error)=>{
//error callback
console.log('error ' , error)
})
}
//get event id from user events
deleteOldEvents=()=>{
let eventsList = [...this.state.userEventsList];
for(var i=0;i<eventsList.length;i++){
this.deleteFromFireBase(eventsList[i].eventId)
}
}
//app users remove that not in contacts
deleteFromFireBase=(eventId)=>{
let timeRemaining = 1;
firebase.database().ref('/events').child(eventId).once('value').then((snapshot) => {
let data = snapshot.val();
let STime = data.sTime; // start time
let eventDate = data.eventDate;
timeRemaining=this.remainingTimeInMin(STime,eventDate);
}).then((data)=>{
//remove from events
if(timeRemaining < 0){
this.setState({removeEventsList:[]});
firebase.database().ref('/events').child(eventId).child('friends').once('value').then((snapshot) => {
let data = snapshot.val();
let items = Object.values(data);
this.setState({removeEventsList:items});
}).then((data)=>{
//remove event data from users node
let removeEventsList = [...this.state.removeEventsList];
for(var i=0;i<removeEventsList.length;i++){
let getPhone= removeEventsList[i].phone
firebase.database().ref('/users').child(getPhone).child('/events').child(eventId).remove();
}
//remove whole event from event node
firebase.database().ref('/events').child(eventId).remove();
}).catch((error)=>{
//error callback
console.log('error ' , error)
})
}
}).catch((error)=>{
//error callback
console.log('error ' , error)
})
}
这给了我“ datetime64 [ns] ”类型。
但是此命令每次都无法正确运行。我遇到了数据混乱的行:
如何正确将格式为“ dd.mm.yyyy 00:00:00 ”的excel列转换为日期类型为“ dd.mm.yyyy < / strong>”来自atat?
P.S。另外,我注意到这种奇怪的情况:原始日期列中的某些值具有str类型,另一种类型为float。但是我无法解决这个问题,因为原始表是从数据库上传的。
答案 0 :(得分:0)
在不指定格式的情况下,pd.to_datetime
必须从数据中猜测如何解释日期字符串。如果使用默认参数,则对于数据的第二行和第三行将失败:
In [5]: date_of_hire = pd.Series(['18.01.2018 0:00:00',
'01.02.2018 0:00:00',
'06.11.2018 0:00:00'])
In [6]: pd.to_datetime(date_of_hire)
Out[6]:
0 2018-01-18
1 2018-01-02
2 2018-06-11
dtype: datetime64[ns]
最快的解决方案是通过dayfirst=True
:
In [7]: pd.to_datetime(date_of_hire, dayfirst=True)
Out[7]:
0 2018-01-18
1 2018-02-01
2 2018-11-06
dtype: datetime64[ns]
如果您知道数据的完整格式,则可以直接指定。仅当格式为完全时才有效,如果行应为缺少转换失败的时间。
In [8]: pd.to_datetime(date_of_hire, format='%d.%m.%Y %H:%M:%S')
Out[8]:
0 2018-01-18
1 2018-02-01
2 2018-11-06
dtype: datetime64[ns]
如果除了日期格式应该保持一致之外,您应该几乎没有关于日期格式的信息,pandas可以事先从数据中推断格式:
In [9]: pd.to_datetime(date_of_hire, infer_datetime_format=True)
Out[9]:
0 2018-01-18
1 2018-02-01
2 2018-11-06
dtype: datetime64[ns]