使用datetime.time进行比较和列创建

时间:2017-10-06 14:35:59

标签: python pandas datetime

我已经使用熊猫一段时间了,我确定这是一个愚蠢的问题。

我需要在数据框中创建一个以datetime.time为条件的列。如果datetime.time< 12,用“早上”填充栏,然后在“下午”#39;和'晚上'#。

import datetime

b['time'] = ['01-01-2000 10:00:00', '01-01-2000 15:00:00', '01-01-2000 21:00:00']

b['time'].dt.time

(output)
1   10:00:00
2   15:00:00
3   21:00:00

b['time'].dt.time < 12 #example

TypeError: can't compare datetime.time to int

问题是:我无法将datetime.time与int进行比较。我该如何解决这个问题?

非常感谢。

1 个答案:

答案 0 :(得分:1)

我认为您可以使用cutnumpy.searchsorted作为二进制文件的标签:

var aText = new Array(
"There are only 10 types of people in the world:",
"Those who understand binary, and those who don't"
);
var iSpeed = 100; // time delay of print out
var iIndex = 0; // start printing array at this posision
var iArrLength = aText[0].length; // the length of the text array
var iScrollAt = 20; // start scrolling up at this many lines

var iTextPos = 0; // initialise text position
var sContents = ''; // initialise contents variable
var iRow; // initialise current row

function typewriter()
{
 sContents =  ' ';
 iRow = Math.max(0, iIndex-iScrollAt);
 var destination = document.getElementById("foo");

 while ( iRow < iIndex ) {
  sContents += aText[iRow++] + '<br />';
 }
 destination.innerHTML = sContents + aText[iIndex].substring(0, iTextPos) + "_";
 if ( iTextPos++ == iArrLength ) {
  iTextPos = 0;
  iIndex++;
  if ( iIndex != aText.length ) {
   iArrLength = aText[iIndex].length;
   setTimeout("typewriter()", 500);
  }
 } else {
  setTimeout("typewriter()", iSpeed);
 }
}
rng = pd.date_range('2017-04-03', periods=24, freq='H')
df = pd.DataFrame({'Date': rng})  

bins = [0, 5, 13, 17, 25]
labels = ['Morning','Afternoon','Evening','Night']
hours = df['Date'].dt.hour
df['bin'] = pd.cut(hours-5+24 *(hours<5),bins=bins,labels=labels,right=False)