pandas to_datetime函数问题

时间:2015-05-29 00:24:17

标签: python datetime pandas

我有一个列是unix时间戳。我想将此列转换为%y-%m-%d格式的日期。只是为了测试我在下面执行的to_datetime()函数,它按预期工作,并以类似2015-05-12 00:11:30的格式给我列:

df['time'] = pd.to_datetime(df['time'], unit='s')

当我添加format参数时如下所示,我收到错误:

df['time'] = pd.to_datetime(df['time'], unit='s', format='%d/%m/%Y')

错误为ValueError: time data 1431389490 does not match format '%d/%m/%Y'

如何剥离小时,分钟和秒钟,以便我只留下2014-05-12?

1 个答案:

答案 0 :(得分:4)

如果您只想提取日期,可以在转换为datetime后的第二步中执行此操作:

datetime.date

这将丢弃有关小时和分钟的信息,但您可以轻松使用生成的列/系列,因为结果是#include<Windows.h> //============================================================================== namespace wreg { //------------------------------------------------------------------------------ using t_oshandle = HKEY; struct t_api { static constexpr auto open_key = ::RegOpenKeyExA; // Tried all of these : // RegOpenKeyExA; &RegOpenKeyExA; (::RegOpenKeyExA); (RegOpenKeyExA); (&RegOpenKeyExA); // static constexpr auto close_key = ::RegCloseKey; }; //------------------------------------------------------------------------------ } // namespace wreg //============================================================================== template < typename T > struct type_deduced; // see Scott Meyers' "Effective Modern C++" #define TYPE_DEDUCED( nr , t ) type_deduced< t > dummy_ ## nr int main () { //type_deduced< decltype(RegOpenKeyExA) > s1; //TYPE_DEDUCED( 1 , decltype(RegOpenKeyExA) ); // 'dummy_1' uses undefined struct 'type_deduced<LSTATUS (HKEY,LPCSTR,DWORD,REGSAM,PHKEY)>' //TYPE_DEDUCED( 1a , decltype(::RegOpenKeyExA) ); // 'dummy_1a' uses undefined struct 'type_deduced<LSTATUS (HKEY,LPCSTR,DWORD,REGSAM,PHKEY)>' //TYPE_DEDUCED( 3 , decltype(wreg::t_api::open_key) ); // 'dummy_3' uses undefined struct 'type_deduced<LSTATUS (__stdcall *const )(HKEY,LPCSTR,DWORD,REGSAM,PHKEY)>' auto hk = wreg::t_oshandle{}; #define ORIGINAL_APPROACH 0 #ifdef ORIGINAL_APPROACH // faulty version auto res = wreg::t_api::open_key( HKEY_LOCAL_MACHINE ,"SOFTWARE" ,0 ,KEY_READ ,&hk ); if (res == ERROR_SUCCESS) { res = wreg::t_api::close_key( hk ); } #else // working version auto open_key = ::RegOpenKeyExA; auto res = open_key( HKEY_LOCAL_MACHINE ,"SOFTWARE" ,0 ,KEY_READ ,&hk ); if (res == ERROR_SUCCESS) { auto close_key = ::RegCloseKey; res = close_key( hk ); } #endif return 0; } //============================================================================== 对象,例如减去以查找列与特定日期之间的天数。

如果您想保留有关小时和分钟的信息,但只是显示,则我不确定是否可能。