How to fix `TypeError: get_sheet_by_name() missing 1 required positional argument: 'name' error` in `openpyxl` - openpyxl

I am trying to display my worksheet names using openpyxl. I get the error "TypeError: get_sheet_by_name() missing 1 required positional argument: 'name'" How can I fix this?
I am on windows 10 OS. I am using Python 3.7 with openpyxl installed pip.
os.chdir(r'C:\Users\zhiva\Desktop')
wb= openpyxl.load_workbook('Book1.xlsx')
wb.get_sheet_by_name()
wb.get_sheet_by_name()
I expected the output as ['Sheet1','Sheet2','Sheet3']

Looks like what you want is:
wb.get_sheet_names()
wb.get_sheet_by_name will get a specific sheet, but you have to pass it the name, hence the error.
See documentation

Can use this as well,
wb_obj.sheetnames
Output :
['Sheet1', 'Sheet2', 'Sheet3']
And in case, if specific worksheet is required, then
wb_obj['Sheet12']

Related

Pandas diff-function: NotImplementedError

When I use the diff function in my snippet:
for customer_id, cus in tqdm(df.groupby(['customer_ID'])):
# Get differences
diff_df1 = cus[num_features].diff(1, axis = 0).iloc[[-1]].values.astype(np.float32)
I get:
NotImplementedError
The exact same code did run without any error before (on Colab), whereas now I'm using an Azure DSVM via JupyterHub and I get this error.
I already found this
pandas pd.DataFrame.diff(axis=1) NotImplementationError
but the solution doesnt work for me as I dont have any Date types. Also I did upgrade pandas but it didnt change anything.
EDIT:
I have found that the error occurs when the datatype is 'int16' or 'int8'. Converting the dtypes to 'int64' solves it.
However I leave the question open in case someone can explain it or show a solution that works with int8/int16.

How to fix Erreur : Subscript `AMr1.orig` is a matrix, the data `x.imp[, -possibleFactors][AMr1.orig]` must have size 1

I'm trying to run Amelia to impute some missing data on several variables with the following code:
set.seed(1)
zz[,c("id", "sex", "team", "minsSocial", "satisTravail", "performance")] <-
Amelia::amelia(zz[,c("id", "sex", "team", "minsSocial", "satisTravail", "performance")],
m=1, idvars="id", noms=c("sex","team"))$imputations$imp1
Unfortunately, I get this error message :
Erreur : Subscript AMr1.orig is a matrix, the data x.imp[, -possibleFactors][AMr1.orig] must have size 1.
Any toughts on where is the problem and how I could fix it? Is it because my data contains values <1?
Thank you!
I think this might be due to some recent changes to error handling in tibbles. If you cast your data as a data.frame instead (assuming that zz is a tibble), the error should go away (this worked for me).
zz <- as.data.frame(zz)
Not sure about the reason behind the error message though. I get a similar error message from rlang::last_error(), and the code worked with earlier versions of the packages.
<error/tibble_error_subset_matrix_must_be_scalar>
Subscript `AMr1.orig` is a matrix, the data `x.imp[AMr1.orig]` must have size 1.
Backtrace:
1. Amelia::amelia(...)
2. Amelia::amelia.default(...)
3. base::lapply(seq_len(m), do.amelia)
4. Amelia:::FUN(X[[i]], ...)
5. Amelia:::impfill(...)
7. tibble:::`[<-.tbl_df`(...)
8. tibble:::tbl_subassign_matrix(x, j, value, j_arg, substitute(value))

How to get an edge's id using TraCi?

I'm using a python code with the traci library to know if there are any vehicles near a certain distance to a chosen vehicle, to test a solution I'm trying to implement I need to know a vehicle's current edge.
I'm on Ubuntu 18.04.3 LTS, using sublime to edit the code and the os, sys, optparse, subprocess, random, math libraries. I've tried using getLaneId and getEdgeId, the last one is not in the documentation but I tough I've seen it somewhere and tried to test it.
. Another option that i had was using getNeighbors but i didn't know exactly how to use it and it returned the same error message as the previous commands.
def run():
step = 0
while traci.simulation.getMinExpectedNumber() > 0:
traci.simulationStep()
print(step)
print(distancia("veh1","veh0"))
step += 1
if step > 2:
print(traci.vehicle.getLaneId("veh0"))
traci.close()
sys.stdout.flush()
All of them returned the following error message : AttributeError: VehicleDomain instance has no attribute 'getLaneId'. But I think the vehicle domain has indeed the getLaneId attribute since it is in the documentation: https://sumo.dlr.de/pydoc/traci._vehicle.html#VehicleDomain-getSpeed.
I was expecting it to return the edge's id. Please I need help with this problem. Thank you in advance.
The TraCI command for edgeID can be found in the _vehicle.VehicleDomain module. The syntax is as follows:
traci._vehicle.VehicleDomain.getRoadID(self, vehicleID)
It needs to be getLaneID with a capital D.

CSV file input not working together with set field value step in Pentaho Kettle

I have a very simple Pentaho Kettle transformation that causes a strange error. It consists of reading a field X from a CSV, add a field Y, set Y=X and finally write it back to another CSV.
Here you can see the steps and the configuration for them:
You can also download the ktr file from here. The input data is just this:
1
2
3
When I run this transformation, I get this error message:
ERROR (version 5.4.0.1-130, build 1 from 2015-06-14_12-34-55 by buildguy) : Unexpected error
ERROR (version 5.4.0.1-130, build 1 from 2015-06-14_12-34-55 by buildguy) : org.pentaho.di.core.exception.KettleStepException:
Error writing line
Error writing field content to file
Y Number : There was a data type error: the data type of [B object [[B©b4136a] does not correspond to value meta [Number]
at org.pentaho.di.trans.steps.textfiIeoutput.TextFiIeOutput.writeRowToFile(TextFiIeOutput.java:273)
at org.pentaho.di.trans.steps.textfiIeoutput.TextFileOutput.processRow(TextFiIeOutput.java:195)
at org.pentaho.di.trans.step.RunThread.run(RunThread.java:62)
atjava.Iang.Thread.run(Unknown Source)
Caused by: org.pentaho.di.core.exception.KettleStepException:
Error writing field content to file
Y Number : There was a data type error: the data type of [B object [[B©b4136a] does not correspond to value meta [Number]
at org.pentaho.di.trans.steps.textfiIeoutput.TextFiIeOutput.writeField(TextFileOutput.java:435)
at org.pentaho.di.trans.steps.textfiIeoutput.TextFiIeOutput.writeRowToFile(TextFiIeOutput.java:249)
3 more
Caused by: org.pentaho.di.core.exception.KettleVaIueException:
Y Number : There was a data type error: the data type of [B object [[B©b4136a] does not correspond to value meta [Number]
at org.pentaho.di.core.row.vaIue.VaIueMetaBase.getBinaryString(VaIueMetaBase.java:2185)
at org.pentaho.di.trans.steps.textfiIeoutput.TextFiIeOutput.formatField(TextFiIeOutput.java:290)
at org.pentaho.di.trans.steps.textfiIeoutput.TextFiIeOutput.writeField(TextFileOutput.java:392)
4 more
All of the above lines start with 2015/09/23 12:51:18 - Text file output.0 -, but I edited it out for brevity. I think the relevant, and confusing, part of the error message is this:
Y Number : There was a data type error: the data type of [B object [[B©b4136a] does not correspond to value meta [Number]
Some further notes:
If I bypass the set field value step by using the lower hop instead, the transformation finish without errors. This leads me to believe that it is the set field value step that causes the problem.
If I replace the CSV file input with a data frame with the same data (1,2,3) everything works just fine.
If I replace the file output step with a dummy the transformation finish without errors. However, if I preview the dummy, it causes a similar error and the field Y has the value <null> on all three rows.
Before I created this MCVE I got the error on all sorts of seemingly random steps, even when there was no file output present. So I do not think this is related to the file output.
If I change the format from Number to Integer, nothing changes. But if I change it to string the transformations finish without errors, and I get this output:
X;Y
1;[B#49e96951
2;[B#7b016abf
3;[B#1a0760b0
Is this a bug? Am I doing something wrong? How can I make this work?
It's because of lazy conversion. Turn it off. This is behaving exactly as designed - although admittedly the error and user experience could be improved.
Lazy conversion must not be used when you need to access the field value in your transformation. That's exactly what it does. The default should probably be off rather than on.
If your field is going directly to a database, then use it and it will be faster.
You can even have "partially lazy" streams, where you use lazy conversion for speed, but then use select values step, to "un-lazify" the fields you want to access, whilst the remainder remain lazy.
Cunning huh?

WLST capture the output of state('ms1') to a variable

I need to capture the output of the below to a variable.
I know we can get to serverRuntime or domainRuntime() trees and get the state. But need to get the below working.
wls:/owb/serverConfig> state('Server1')
Current state of 'Server1' : RUNNING
I tried two ways:
wls:/owb/serverConfig> print state('Server1')
Current state of 'Server1' : RUNNING
None
wls:/owb/serverConfig> x=state('Server1')
Current state of 'Server1' : RUNNING
wls:/owb/serverConfig> print x
None
You have to use the getState() method of server runtime mbean.
You can obtain the server runtime mbean by navigating into wlst runtime tree or by using a lookup method.
Sample:
domainRuntime()
slrBean = cmo.lookupServerLifeCycleRuntime('Server1')
status = slrBean.getState()
print 'Status of Managed Server is '+status
See also Getting Runtime Information in WLST official documentation.
This same question was raised by Dianyuan Wang with me 2011.
Here is the steps to resolve your issue.
1. Capture the output of state command using redirect, stopRedirect command
2. Use the Python regular expression in search function to extract the desired server output.
Code snippet is here
fileName='/tmp/myserver_state.txt'
redirect(fileName)
state(server_nm,'Server')
stopRedirect()
f = open(fileName)
try:
for line in f.readlines():
if re.search('Current state',line):
status[server_nm]=line
except:
continue
Now you can apply desired logic after this block.
Cheers!!
HTH
Here is what I am using and is working like charm
cd("/ServerRuntimes/ms1")
state=cmo.getState()
print state