首先我要说的是,我对numpy和pandas相当新鲜。我试图构建一个大熊猫数据框,但我不确定我是否以适当的方式做事。
我的设置是我有一个很大的.Net对象列表(我几乎无法控制),我想用pandas数据框建立一个时间序列。我有一个例子,我用简化的占位符类替换.Net类只是为了演示。代码中的listOfthings
基本上是我从.Net获得的,我想将其转换为pandas数据帧。
我的问题是:
np.datetime64(str(np.datetime64('now','us')))
。代码示例:
import numpy as np
import pandas as pd
class PlaceholderClass:
def time(self):
return str(np.datetime64('now', 'us'))
def value(self):
return 100*np.random.random_sample()
listOfThings = [PlaceholderClass() for i in range(1000)]
arr = np.array([(x.time(), x.value()) for x in listOfThings], dtype=[('time', np.datetime64), ('value', np.float)])
dataframe = pd.DataFrame(data=arr['value'], index=arr['time'])
提前致谢
答案 0 :(得分:1)
<强> Q1:强>
我认为没有必要首先创建一个np.array然后创建数据帧。这完全没问题,例如:
private SerialPort _serialPort;
private int _baudRate = 115200;
string unread = "AT+CMGL=\"REC UNREAD\"\r";
string read = "AT+CMGL=\"REC READ\"\r";
string all = "AT+CMGL=\"ALL\"\r";
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
SMS(all);
}
private void SMS(string cmd)
{
try
{
string[] ports = SerialPort.GetPortNames();
_serialPort = new SerialPort("COM3", _baudRate);
_serialPort.Dispose();
_serialPort.Parity = Parity.None;
_serialPort.DataBits = 8;
_serialPort.StopBits = StopBits.One;
_serialPort.Handshake = Handshake.XOnXOff;
_serialPort.DtrEnable = true;
_serialPort.RtsEnable = true;
_serialPort.NewLine = Environment.NewLine;
_serialPort.Close();
//_serialPort.
if (!_serialPort.IsOpen)
{
_serialPort.Open();
}
_serialPort.Write("AT" + System.Environment.NewLine);
Thread.Sleep(1000);
_serialPort.WriteLine("AT+CMGF=1" + System.Environment.NewLine);
Thread.Sleep(1000);
_serialPort.WriteLine(cmd + System.Environment.NewLine);
Thread.Sleep(3000);
var res = _serialPort.ReadExisting();
MessageBox.Show( _serialPort.ReadExisting());
//string data=_serialPort.ReadExisting();
//tb_messages.Text = data;
_serialPort.Close();
//Regex r = new Regex(@"\+CMGL:(\d+),""(.+)"",""(.+)"",(.*),""(.+)""\r\n(.+)\r\n");
//Match m = r.Match(_serialPort.ReadExisting());
//string a = m.Groups[1].Value;
//string b = m.Groups[2].Value;
//string c = m.Groups[3].Value;
//MessageBox.Show(_serialPort.ReadExisting());
}
catch (Exception e)
{
//throw;
MessageBox.Show(e.ToString());
}
}
稍后添加:
rd = lambda: datetime.date(randint(2005,2025), randint(1,12),randint(1,28))
df = pd.DataFrame([(rd(), rd()) for x in range(100)])
<强> Q2:强>
我注意到pd.to_datetime('某个日期')几乎总是正确的。即使没有指定格式。也许这有帮助。
df = pd.DataFrame((x.value() for x in listOfThings), index=(pd.to_datetime(x.time()) for x in listOfThings))