将timedeltas转换为天错误

时间:2016-07-19 20:37:11

标签: python pandas

我在将<?php namespace App\Auth; use Illuminate\Auth\GenericUser; use Illuminate\Contracts\Auth\Authenticatable as Authenticatable; use Illuminate\Contracts\Auth\UserProvider; use Psr\Http\Message\ResponseInterface; use Illuminate\Http\Request; use App\XXX\Facades\XXXX; use Log; class CustomUserProvider implements UserProvider { public $errors = []; public function getErrorMessages ( ) { return $this->errors; } public function retrieveById($identifier) { // TODO: Implement retrieveById() method. } public function retrieveByToken($identifier, $token) { // TODO: Implement retrieveByToken() method. return null; } public function updateRememberToken(Authenticatable $user, $token) { // TODO: Implement updateRememberToken() method. } public function retrieveByCredentials(array $credentials) { $response =APIService::get( 'GET', $credentials, 'tokens'); if ( array_key_exists('code', $response) ) { /* Exception o Credentials Error */ $this->errors = $response; return null; } else { $apiCredentials = json_decode( $response->getBody(), true ); if( $apiCredentials ) { $attributes = array ( 'id' => 1, 'name' => 'Name', 'lastname' => 'LastName', 'email' => 'email@email.com' ); $user = new GenericUser( $attributes ); return $user; } } return null; } public function validateCredentials(Authenticatable $user, array $credentials) { if( $user->email == $credentials['email'] ) { return true; } return false; } } 转换为timedeltas时遇到了问题。

int (days)

*输入:*

df.FIN = pd.to_datetime(df.FIN, errors = 'coerce')
df.START = pd.to_datetime(df.START, errors = 'coerce')
df["FIN-START"] = df["FIN"] - df["START"]

*导致错误的行:*

0    0 days 00:00:00
1   89 days 19:41:05
2    0 days 00:00:00
3    0 days 00:00:00
4    0 days 00:00:00
...
Name: FIN-START, dtype: timedelta64[ns]

*错误:*

df["days"] = df["FIN-START"].apply(lambda td: td.days)

1 个答案:

答案 0 :(得分:0)

对于Series中的每个元素,将timedelta转换为timedelta[D],例如94 days。然后,只需要它astype int。

df["days"] = df["FIN-START"].apply(lambda td: td.astype('timedelta64[D]').astype(int))