小编jor*_*ris的帖子

将乳胶转换为rst/sphinx

我正在使用rst和sphinx编写项目文档.但是之前,我已经在乳胶中写了一部分,现在想把它转换为rst,然后将它添加到文档中.rst与乳胶的融合是显而易见的,但谷歌搜索相反的方式并没有直接产生一些东西.

所以我的问题是:有没有办法将乳胶转化为rst?

编辑
理想情况下,我想将其转换为rST,专门用于sphinx.因此,如果它可以使用例如sphinx的扩展来显示数学,那将是很好的.

latex restructuredtext

6
推荐指数
1
解决办法
4552
查看次数

pandas 重新采样数据帧并将日期时间索引保留为列

我正在尝试使用 pandas 将每日数据重新采样为每周数据。

我正在使用以下内容:

weekly_start_date =pd.Timestamp('01/05/2011')
weekly_end_date =pd.Timestamp('05/28/2013')

daily_data = daily_data[(daily_data["date"] >= weekly_start_date) & (daily_data["date"] <= weekly_end_date)]    

daily_data = daily_data.set_index('date',drop=False)
weekly_data = daily_data.resample('7D',how=np.sum,closed='left',label='left')
Run Code Online (Sandbox Code Playgroud)

问题是weekly_data 不再有日期列。

我错过了什么?

谢谢,

pivot pandas

5
推荐指数
2
解决办法
9109
查看次数

使用 Python 计算每小时频率

我有这样的每小时 csv 数据,每天都这样排序数百天:

2011.05.16,00:00,1.40893
2011.05.16,01:00,1.40760
2011.05.16,02:00,1.40750
2011.05.16,03:00,1.40649

我想计算每天设置的最大值每小时多少次,所以如果在 00:00 我有 2011.05.16 天的最大值,我将 1 添加到 00:00 等等。为此,我使用了一个循环来以这种方式计算像索引一样的小时数:

def graph():    
Date, Time,  High = np.genfromtxt(myPath, delimiter=",",
                                  unpack = True,  converters={0:date_converter})                                                                           
numList = [""] * 24
index=0
hour=0    
count = [0] * 24

for eachHour in Time:        
    numList[hour] += str(High[index])        
    index += 1
    hour +=1        

    if hour == 24:           
        higher = (numList.index(max(numList)))
        count[higher] += 1            
        hour = 0            
        numList = [""] * 24
Run Code Online (Sandbox Code Playgroud)

问题是,在我的数据中,经常有一些小时缺失的差距,但循环无法识别它并继续将值放入下一小时的索引中。我到处搜索,但我是编程新手,这是我的第一个“复杂”工作,所以我需要更具体的答案来了解它是如何工作的。那么你如何像解释的那样进行每小时频率计数? 最终结果应该是这样的:

00:00 n time max of the …
Run Code Online (Sandbox Code Playgroud)

python time pandas

5
推荐指数
1
解决办法
5648
查看次数

为什么pandas在调用pd.to_datetime()时会返回时间戳而不是datetime对象?

根据手册,pd.to_datetime()应该创建一个datetime对象.

相反,当我打电话时pd.to_datetime("2012-05-14"),我得到一个时间戳对象!调用to_datetime()该对象最终给了我一个datetime对象.

In [1]: pd.to_datetime("2012-05-14")
Out[1]: Timestamp('2012-05-14 00:00:00', tz=None)

In [2]: t = pd.to_datetime("2012-05-14")
In [3]: t.to_datime()
Out[2]: datetime.datetime(2012, 5, 14, 0, 0)
Run Code Online (Sandbox Code Playgroud)

对这种意外行为有解释吗?

python datetime timestamp pandas

5
推荐指数
1
解决办法
1481
查看次数

将PeriodIndex转换为DateTimeIndex?

我有一个关于将tseries.period.PeriodIndex转换为日期时间的问题.

我有一个DataFrame,如下所示:

               colors      country
time_month 

2010-09         xxx        xxx
2010-10         xxx        xxx
2010-11         xxx        xxx
...

time_month是索引.

type(df.index)
Run Code Online (Sandbox Code Playgroud)

回报

class 'pandas.tseries.period.PeriodIndex'
Run Code Online (Sandbox Code Playgroud)

当我尝试使用df进行VAR分析时(http://statsmodels.sourceforge.net/devel/vector_ar.html#vector-autoregressions-tsa-vector-ar),

VAR(mdata)
Run Code Online (Sandbox Code Playgroud)

收益:

Given a pandas object and the index does not contain dates
Run Code Online (Sandbox Code Playgroud)

显然,Period不被认为是日期时间.现在,我的问题是如何将索引(time_month)转换为VAR分析可以使用的日期时间?

df.index = pandas.DatetimeIndex(df.index)
Run Code Online (Sandbox Code Playgroud)

回报

cannot convert Int64Index->DatetimeIndex
Run Code Online (Sandbox Code Playgroud)

谢谢您帮忙!

python datetime-format dataframe pandas

5
推荐指数
2
解决办法
9386
查看次数

如何在numpy中将ndarray的dtype更改为自定义的?

我做了一个dtype:

mytype = np.dtype([('a',np.uint8), ('b',np.uint8), ('c',np.uint8)])
Run Code Online (Sandbox Code Playgroud)

所以使用这个dtype的数组:

test1 = np.zeros(3, dtype=mytype)
Run Code Online (Sandbox Code Playgroud)

test1是:

array([(0, 0, 0), (0, 0, 0), (0, 0, 0)],
      dtype=[('a', '|u1'), ('b', '|u1'), ('c', '|u1')])
Run Code Online (Sandbox Code Playgroud)

现在我有test2:

test2 = np.array([[1,2,3], [4,5,6], [7,8,9]])
Run Code Online (Sandbox Code Playgroud)

当我使用时test2.astype(mytype),结果不是我想要的结果:

array([[(1, 1, 1), (2, 2, 2), (3, 3, 3)],
       [(4, 4, 4), (5, 5, 5), (6, 6, 6)],
       [(7, 7, 7), (8, 8, 8), (9, 9, 9)]],
      dtype=[('a', '|u1'), ('b', '|u1'), ('c', '|u1')])
Run Code Online (Sandbox Code Playgroud)

我希望结果如下:

array([(1, 2, 3), (4, 5, 6), (7, 8, 9)],
      dtype=[('a', …
Run Code Online (Sandbox Code Playgroud)

python numpy recarray

4
推荐指数
1
解决办法
481
查看次数

使用 Pandas 读取使用 h5py 创建的 HDF5 文件

我有一堆 hdf5 文件,我想将其中的一些数据转换为 parquet 文件。不过,我正在努力将它们读入 pandas/pyarrow 中。我认为这与文件最初创建的方式有关。

如果我使用 h5py 打开文件,数据看起来完全符合我的预期。

import h5py

file_path = "/data/some_file.hdf5"
hdf = h5py.File(file_path, "r")
print(list(hdf.keys()))
Run Code Online (Sandbox Code Playgroud)

给我

>>> ['foo', 'bar', 'baz']
Run Code Online (Sandbox Code Playgroud)

在本例中,我对“bar”组感兴趣,其中包含 3 个项目。

如果我尝试读取使用中的数据,HDFStore我将无法访问任何组。

>>> ['foo', 'bar', 'baz']
Run Code Online (Sandbox Code Playgroud)

那么该HDFStore对象就没有键或组。

import pandas as pd

file_path = "/data/some_file.hdf5"
store = pd.HDFStore(file_path, "r")
Run Code Online (Sandbox Code Playgroud)

如果我尝试访问数据,则会收到以下错误

assert not store.groups()
assert not store.keys()
Run Code Online (Sandbox Code Playgroud)
TypeError: cannot create a storer if the object is not existing nor a value are passed
Run Code Online (Sandbox Code Playgroud)

同样,如果我尝试使用pd.read_hdf它看起来文件是空的。

bar = store.get("/bar")
Run Code Online (Sandbox Code Playgroud)
ValueError: Dataset(s) …
Run Code Online (Sandbox Code Playgroud)

python h5py pandas hdfstore

4
推荐指数
1
解决办法
4453
查看次数

如何在matplotlib中的pandas bar图上添加一行?

嗨我已设法在条形图中添加一条线,但位置不对.我想在每个栏的正中间做点.有人可以帮忙吗?

>>> df
   price       cost        net
0   22.5 -20.737486   1.364360
1   35.5 -19.285862  16.695847
2   13.5 -20.456378  -9.016052
3    5.0 -19.643776 -17.539636
4   13.5 -27.015138 -15.964597
5    5.0 -24.267836 -22.618819
6   18.0 -21.096404  -7.357684
7    5.0 -24.691966 -24.116106
8    5.0 -25.755958 -22.080329
9   25.0 -26.352161  -2.781588

fig = plt.figure()
df[['price','cost']].plot(kind = 'bar',stacked = True,color = ['grey','navy'])
df['net'].plot('o',color = 'orange',linewidth=2.0,use_index = True)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

python plot matplotlib pandas

3
推荐指数
1
解决办法
5661
查看次数

在没有循环的情况下将日期时间序列转换为pandas中的字符串

我有一个日期时间系列作为Pandas数据帧中的一列 - df ['timeStamp'].我想将此列中的每个日期时间对象转换为字符串.有几个python函数将单个日期时间对象转换为字符串,如:

str(datetime)
datetime.format(datetime) 
datetime.strftime('%m%d%Y')
Run Code Online (Sandbox Code Playgroud)

但是为了获得我想要的东西,我必须遍历整个列并将每个对象单独转换为字符串.我试图避免的东西.我想知道是否有任何Pandas/Python函数可以一次性完成.与to_datetime函数相反的东西(在没有循环的情况下将str转换为datetime).

python string datetime pandas

3
推荐指数
1
解决办法
3313
查看次数

pandas to_sql 方法给出日期列错误

我有一个看起来像这样的数据框:

df = pd.DataFrame(index= pd.date_range('2014-01-01', periods=10))
df['date'] = df.index.map(lambda x: x.strftime('%d-%m-%Y'))
df['date'] = df.index
df['profit']= rand(10)
df['perf_period_id']=2
Run Code Online (Sandbox Code Playgroud)

还有一个 sqlite3 db 和一个名为 fee_profit 的表

费用_利润有 4 个字段:

  • id - 整数 - 主键
  • perf_period_id - 整数
  • 日期 - 日期
  • 利润 - 真实

当我尝试将数据帧写入数据库时​​(不显示数据库连接):

df.to_sql(name='fee_profit', index=False, con=db, if_exists='append')
Run Code Online (Sandbox Code Playgroud)

我得到以下代码:

252     else:
253         data = [tuple(x) for x in frame.values.tolist()]
--> 254     cur.executemany(insert_query, data)
255 
256 
InterfaceError: Error binding parameter 0 - probably unsupported type.
Run Code Online (Sandbox Code Playgroud)

没有传递主键(这可能是问题吗?)我把桌子弄得乱七八糟,看起来肯定是日期有问题。尝试了在索引中传递日期的各种组合,也是字符串,没有任何效果。

知道我哪里出错了。无法在任何地方找到使用此方法的示例。

使用 Pandas 0.13.1 和 sqlite 3 2.6.0。数据库是通过 …

python sql sqlite pandas

2
推荐指数
1
解决办法
1万
查看次数