我DataFrame
在DateTime
索引中有一个列,代表如下的四分之一:
2000-03-31 00:00:00
Run Code Online (Sandbox Code Playgroud)
如何将其转换为'2000q1'?
我查看了文档,但他们只说DateTimeIndex.quarter这里:http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DatetimeIndex.quarter.html
format='%Y%q'
不起作用.也没有选择on='%Y%q
我在 Python 3.X 中有一张简单的图片,我可以显示它。但是,我无法使用 skimage.transform.rotate 旋转它。
错误是我的“图片”对象没有形状。我已经看到旋转命令后包含 .shape,但它仍然不起作用。
这是我的代码
import skimage
from skimage import novice
# New, all black picture
pic = skimage.novice.Picture.from_size((600, 600), color=(0, 0, 0))
# Coloring some pixels in white
for i in range(0, len(all_dots) - 1, 2):
x = all_dots[i]
y = all_dots[i + 1]
pic[x, y] = (255, 255, 255)
from skimage.transform import rotate
new_pic = rotate(pic, 180)
# Also new_pic = rotate(pic, 180).shape does not work
new_pic.show()
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?谢谢
我有一个(大)名单,里面有男性和女性特工。
我想对每个应用不同的功能。
在这种情况下如何使用 Pool ?鉴于代理是相互独立的。
一个例子是:
males = ['a', 'b', 'c']
females = ['d', 'e', 'f']
for m in males:
func_m(m)
for f in females:
func_f(f)
Run Code Online (Sandbox Code Playgroud)
我是这样开始的:
from multiprocessing import Pool
p = Pool(processes=2)
p.map() # Here is the problem
Run Code Online (Sandbox Code Playgroud)
我想要一些类似的东西:
p.ZIP(func_f for f in females, func_m for m in males) # pseudocode
Run Code Online (Sandbox Code Playgroud) 我无法在这里实现这个建议:同时将两个函数应用于两个列表.
我想这是因为模块是由另一个模块导入的,因此我的Windows会生成多个python进程?
我的问题是:如果没有if,我怎么能使用下面的代码 if __name__ == "__main__":
args_m = [(mortality_men, my_agents, graveyard, families, firms, year, agent) for agent in males]
args_f = [(mortality_women, fertility, year, families, my_agents, graveyard, firms, agent) for agent in females]
with mp.Pool(processes=(mp.cpu_count() - 1)) as p:
p.map_async(process_males, args_m)
p.map_async(process_females, args_f)
Run Code Online (Sandbox Code Playgroud)
这两个process_males
和process_females
是fuctions.
args_m, args_f
是迭代器
另外,我不需要退货.代理是需要更新的类实例.
为了避免重复,我想做这样的事情:
a, b = True, False
l = list()
for op in [and, or, xor]:
l.append(a op b)
Run Code Online (Sandbox Code Playgroud)
我import operator
也试过和itertools
,但它们不包含逻辑运算符,只包含数学和其他一些运算符。
我找不到任何有用的先前答案!
我不是计算机科学家,我一直在努力使基于代理的模型相当快(在具有大量输入的 for 循环上实现池,多处理池示例(并行)比顺序慢。尝试了解 python 中的池,返回实例列表没有区别?那么性能呢?)
每个人都一直告诉我要进行个人简介。我又试了一次,但我不知道如何解释结果(使用timeit
我知道哪个函数较慢,但我无法使用Pool
or @jit
)。
那么鉴于以下结果,我应该如何进行?非常感谢。
98008290 function calls (96393650 primitive calls) in 568.193 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 <decorator-gen-0>:1(<module>)
1 0.000 0.000 0.000 0.000 <decorator-gen-10>:1(<module>)
1 0.000 0.000 0.000 0.000 <decorator-gen-11>:1(<module>)
1 0.000 0.000 0.000 0.000 <decorator-gen-1>:1(<module>)
6 0.000 0.000 0.006 0.001 <decorator-gen-1>:1(non_reentrant)
1 0.000 0.000 0.000 0.000 <decorator-gen-2>:1(<module>)
1 0.000 0.000 0.000 0.000 <decorator-gen-3>:1(<module>) …
Run Code Online (Sandbox Code Playgroud) 我使用geopandas
和colormap
代码绘制地图:
import numpy as np
## make up some random data
df = pd.DataFrame(np.random.rand(20, 3), columns=['x', 'y', 'val'])
df['geometry'] = df.apply(lambda row: shapely.geometry.Point(row.x, row.y), axis=1)
# make it a geopandas DataFrame
gdf = gpd.GeoDataFrame(df)
## the plotting
ax = gdf.plot(column='val', colormap='hot', vmin=vmin, vmax=vmax)
# add colorbar
fig = ax.get_figure()
cax = fig.add_axes([0.9, 0.1, 0.03, 0.8])
sm = plt.cm.ScalarMappable(cmap='hot', norm=plt.Normalize(vmin=0, vmax=1))
# fake up the array of the scalar mappable. Urgh...
sm._A = []
fig.colorbar(sm, cax=cax)
Run Code Online (Sandbox Code Playgroud)
然后,我得到这个图(数据与上面的示例不同) …
python ×7
datetime ×1
format ×1
geopandas ×1
matplotlib ×1
pandas ×1
performance ×1
profile ×1
python-3.x ×1
save ×1
scikit-image ×1
zoom ×1