Issues in printing time series graph from pandas dataframe - matplotlib

I have a DataFrame object with a datetime as index:
In [210]:
f
f.plot(legend=True)
Out[210]:
user_time sys_time wait_io_time
date_time
2012-11-01 08:59:27 3 1 0
2012-11-01 08:59:32 0 0 0
2012-11-01 08:59:37 20 2 1
2012-11-01 08:59:42 0 0 0
2012-11-01 08:59:47 0 0 0
f.plot() causes this error:
-> 1367 plot_obj.generate()
--> 674 self._make_plot()
-> 1000 self._make_ts_plot(data, **self.kwds)
---> 81 left, right = _get_xlim(ax.get_lines())
--> 220 left = min(x[0].ordinal, left)
AttributeError: 'numpy.int64' object has no attribute 'ordinal'
I do see one line graph, displaying values from the user_time data, so suspecting that the data values from sys_time may be causing the issue. Following suggestion from https://github.com/pydata/pandas/issues/1737, I installed a newer version of matplotlib (1.1.1) but no luck, and plotting fails on the data from the bug url as well.

This seems to work with latest matplotlib library installed. I forgot to restart the ipython notebook process or reload the matplotlib after I upgraded it.

Related

Convert pandas dataframe with missing values from object to int

I am trying to change the values in the data frame below to ints so I can changes these times hh/mm/ss into a number value based on hours (e.g. for row two hrs_cor would equal 5.5).
hrs mins secs
0 None None
1 None None
2 5 30 00
3 5 22 30
4 8 00 00
... .. ... ...
1052 None None
1053 None None
1054 None None
1055 None None
1056 None None
The issue I am running is converting the data frame into numeric values, and I think it is due to the empty cells. So far I have tried variations of the code below:
MID_calc['hrs'] = MID_calc.to_numeric(MID_calc['hrs'], errors='coerce').astype('INT46')
And this error is returned:
AttributeError: 'DataFrame' object has no attribute 'to_numeric'
Currently, all values are objects
hrs object
mins object
secs object
dtype: object
I have looked through several posts, but nothing seems to be working. Any help would be greatly appreciated!
You need to use
import pandas as pd
MID_calc['hrs'] = pd.to_numeric(MID_calc['hrs'] , errors='ignore').astype('INT46')
dont just copy the code directly from the website. Understand what it does.

Getting errors trying to change the values of a column with conditions

Here's my data:
id medianHouseValue housingMedianAge totalBedrooms totalRooms \
0 23 113.903 31.0 543.0 2438.0
1 24 99.701 56.0 337.0 1692.0
2 26 107.500 41.0 123.0 535.0
3 27 93.803 53.0 244.0 1132.0
4 28 105.504 52.0 423.0 1899.0 households population medianIncome
0 481.0 1016.0 1.7250
1 328.0 856.0 2.1806
2 121.0 317.0 2.4038
3 241.0 607.0 2.4597
4 400.0 1104.0 1.8080
Here's what I'm trying to do:
change any values in the medianIncome column that are 0.4999 or lower to 0.4999 and change any values that are 15.0001 and higher to 15.0001.
I've tried this:
housing.loc[housing[‘medianIncome'] > 15.0001, 'medianIncome'] = 15.0001
housing.loc[housing[‘medianIncome'] < 0.4999, 'medianIncome'] = 0.4999
And get this error:
AttributeError: 'list' object has no attribute 'loc'
So then I tried this:'
housing['medianIncome'] = np.where(housing['medianIncome'] >= 15.0001, housing['medianIncome'])
housing['medianIncome'] = np.where(housing['medianIncome'] <= 0.4999, housing['medianIncome'])
And get this error:
TypeError: list indices must be integers or slices, not str
I've looked up both errors but can't seem to find a solution that will accommodate. There's a lot more rows, it's just not letting me co[y/paste them all here and I can't recall how to upload the data set.

Removing the .0 from a pandas column

After a simple merge of two dataframes the following X column becomes an object and an ".0" is being added at the end for no apparent reason. I tried replacing the nan values with an integer and then converting the whole column to an integer hoping for the .0 to be gone. The code runs but it doesn't really change the dtype of that column. Also, I tried removing the .0 with the rstrip command but then all it really does is it removes everything and even the values that are 249123.0 become NaN which doesn't make sense. I know that is a very basic issue but I am not sure what else could I try at this point.
Input:
Age ID
22 23105.0
34 214541.0
51 0
8 62341.0
Desired output:
Age ID
22 23105
34 214541
51 0
8 62341
Any ideas would be much appreciated.
One of the ways to get rid of the trailing .0 in an object column is to use pandas.DataFrame.replace :
df['ID'] = df['ID'].replace(r'\.0$', '', regex=True).astype(np.int64)
# Output :
print(df)
Age ID
0 22 23105
1 34 214541
2 51 0
3 8 62341

How to deal with the error when using Gurobi with cvxpy :Unable to retrieve attribute 'BarIterCount'

How to deal with the error when using Gurobi with cvxpy :AttributeError: Unable to retrieve attribute 'BarIterCount'.
I have an Integer programming problem, using cvxpy and set gurobi as a solver.
When the number of variables is small, the result is ok. After the number of variables reaches a level of like 43*13*6, then the error occurred. I suppose it may be caused by the scale of the problem, in which the gurobi solver can not estimate the BarIterCount, which is the max Iterations needed.
Thus, I wonder, is there any way to manually set the BarItercount attribute of gurobi through the interface of the CVX? Or whether there exists another way to solve this problem?
Thanks for any suggestions you may provide for me.
The trace log is as follows:
If my model is small, like I set a number which indicates the scale of model as 3, then the program is ok. The trace is :
Using license file D:\software\lib\site-packages\gurobipy\gurobi.lic
Restricted license - for non-production use only - expires 2022-01-13
Parameter OutputFlag unchanged
Value: 1 Min: 0 Max: 1 Default: 1
D:\software\lib\site-packages\cvxpy\reductions\solvers\solving_chain.py:326: DeprecationWarning: Deprecated, use Model.addMConstr() instead
solver_opts, problem._solver_cache)
Changed value of parameter QCPDual to 1
Prev: 0 Min: 0 Max: 1 Default: 0
Gurobi Optimizer version 9.1.0 build v9.1.0rc0 (win64)
Thread count: 16 physical cores, 32 logical processors, using up to 32 threads
Optimize a model with 126 rows, 370 columns and 2689 nonzeros
Model fingerprint: 0x70d49530
Variable types: 0 continuous, 370 integer (369 binary)
Coefficient statistics:
Matrix range [1e+00, 7e+00]
Objective range [1e+00, 1e+00]
Bounds range [1e+00, 1e+00]
RHS range [1e+00, 6e+00]
Found heuristic solution: objective 7.0000000
Presolve removed 4 rows and 90 columns
Presolve time: 0.01s
Presolved: 122 rows, 280 columns, 1882 nonzeros
Variable types: 0 continuous, 280 integer (279 binary)
Root relaxation: objective 4.307692e+00, 216 iterations, 0.00 seconds
Nodes | Current Node | Objective Bounds | Work
Expl Unexpl | Obj Depth IntInf | Incumbent BestBd Gap | It/Node Time
0 0 4.30769 0 49 7.00000 4.30769 38.5% - 0s
H 0 0 6.0000000 4.30769 28.2% - 0s
0 0 5.00000 0 35 6.00000 5.00000 16.7% - 0s
0 0 5.00000 0 37 6.00000 5.00000 16.7% - 0s
0 0 5.00000 0 7 6.00000 5.00000 16.7% - 0s
Cutting planes:
Gomory: 4
Cover: 9
MIR: 4
StrongCG: 1
GUB cover: 9
Zero half: 1
RLT: 1
Explored 1 nodes (849 simplex iterations) in 0.12 seconds
Thread count was 32 (of 32 available processors)
Solution count 2: 6 7
Optimal solution found (tolerance 1.00e-04)
Best objective 6.000000000000e+00, best bound 6.000000000000e+00, gap 0.0000%
If the number is 6, then error occurs:
-------------------------------------------------------
Using license file D:\software\lib\site-packages\gurobipy\gurobi.lic
Restricted license - for non-production use only - expires 2022-01-13
Parameter OutputFlag unchanged
Value: 1 Min: 0 Max: 1 Default: 1
D:\software\lib\site-packages\cvxpy\reductions\solvers\solving_chain.py:326: DeprecationWarning: Deprecated, use Model.addMConstr() instead
solver_opts, problem._solver_cache)
Changed value of parameter QCPDual to 1
Prev: 0 Min: 0 Max: 1 Default: 0
Gurobi Optimizer version 9.1.0 build v9.1.0rc0 (win64)
Thread count: 16 physical cores, 32 logical processors, using up to 32 threads
Traceback (most recent call last):
File "model.py", line 274, in <module>
problem.solve(solver=cp.GUROBI,verbose=True)
File "D:\software\lib\site-packages\cvxpy\problems\problem.py", line 396, in solve
return solve_func(self, *args, **kwargs)
File "D:\software\lib\site-packages\cvxpy\problems\problem.py", line 754, in _solve
self.unpack_results(solution, solving_chain, inverse_data)
File "D:\software\lib\site-packages\cvxpy\problems\problem.py", line 1058, in unpack_results
solution = chain.invert(solution, inverse_data)
File "D:\software\lib\site-packages\cvxpy\reductions\chain.py", line 79, in invert
solution = r.invert(solution, inv)
File "D:\software\lib\site-packages\cvxpy\reductions\solvers\qp_solvers\gurobi_qpif.py", line 59, in invert
s.NUM_ITERS: model.BarIterCount,
File "src\gurobipy\model.pxi", line 343, in gurobipy.gurobipy.Model.__getattr__
File "src\gurobipy\model.pxi", line 1842, in gurobipy.gurobipy.Model.getAttr
File "src\gurobipy\attrutil.pxi", line 100, in gurobipy.gurobipy.__getattr
AttributeError: Unable to retrieve attribute 'BarIterCount'
Hopefully this can provide more hint for solution.
BarIterCount is the number of barrier iterations performed to solve an LP. This is not a limit on the number of iterations and it should only be queried when the current optimization process has been finished. You cannot set this attribute either, of course.
To actually limit the number of iterations the barrier algorithm is allowed to take, you can use the parameter BarIterLimit.
Please inspect your log file for further information about the solver's behavior.

optaplanner vrp file with road time and time window

I am trying to create VRP file which defines a problem with time window and distance in seconds. I currently do not need capacity (can I turn it off?)
this is my file :
NAME: almirs-test
COMMENT: Generated for OptaPlanner Examples
TYPE: CVRPTW
DIMENSION: 2
EDGE_WEIGHT_TYPE: EXPLICIT
EDGE_WEIGHT_FORMAT: FULL_MATRIX
EDGE_WEIGHT_UNIT_OF_MEASUREMENT: sec
CAPACITY: 125
NODE_COORD_SECTION
0 0 0 BRUSSEL
55 1 1 ANTHISNES
EDGE_WEIGHT_SECTION
0.0 1
1 0.0
DEMAND_SECTION
0 0 0 100 0
55 1 0 10 1
DEPOT_SECTION
0
-1
EOF
it is corcectly parsed, and I see locations on screen, but when I try to solve it I get message : "Not feasible"
org.optaplanner.examples.vehiclerouting.solver/arrivalAfterDueTime/level0/[ANTHISNES]=-990
any idea what am I doing wrong? any samples where I can see how it is done?
thanks
almir