How to "FillMissing" with a value of 0 - c#

I'm reading some data in from a CSV file into a frame and I want to replace the blanks in a certain column with zeros. However, when I do FillMissing(0), the series returned still shows the values as blanks. I'm guessing it's because Deedle inferred the type of the column to be int and not int? and thus a zero is equivalent to missing.
Is there a way to either use FillMissing to do what I want, or alternatively, override the type inference so it treats this column as an int??

The FillMissing method will fill all missing values in columns that have the same type as the value provided. This is a bit confusing and we're looking for better ideas how to do this!
This means that FillMissing(0) will only fill columns with integers. You can try calling FillMissing(0.0) to handle floating point columns or FillMissing(0.0M) to handle decimals.
The fact whether a value is nullable does not matter - Deedle handles missing values directly and so column loaded from a CSV will never have a type int?

Related

Testing database field types for compatibility

I have some generic database routines. One that I make use of quite frequently is based on code similar to this code, but for an OleDbDataReader, not the generic IDataReader from that code.
I was playing around with this, and decided to test what would if I tried to (say) retrieve a value from a field, where I had input the incorrect type. Eg: I try to get a double value from a database column that is actually an integer:
reader.GetValue<double>("Column_that_is_Integer_Type");
...and, unexpectedly, it seamlessly converts the database column integer value to a double. Hmm. OK - I get a usable value back, but what about other conversions?
reader.GetValue<bool>("Column_that_is_Integer_Type");
This returns true. Not exactly what I want, and I get no error.
reader.GetValue<DateTime>("Column_that_is_Integer_Type");
This one at least throws an InvalidCast Exception.
Because of all this, I added the follow type checks to the code:
if (theReader.GetFieldType(fieldIndex) == typeof(T))
{
//Carry on...
}
else
{
//Raise an error
}
I think this is the safest way of preventing issues, but was wondering if there is a somewhat 'generic' compatibility check that can be performed? My onward use of a given variable probably won't care whether '39.5' retrieved from a database that stores it as a double is passed around as a decimal, but it will certainly care if it is being passed around as a bool.
My default position will be to throw an error if someone gets the column data type wrong, but I was interested enough to ask the question: Is there a robust method for checking whether type conversions preserve appropriate data integrity?
Eg. Integer type converted to Double: OK. Double converted to Integer: Nope.
Double type converted to Decimal: OK. Double converted to DateTime: Nope.
I think I've answered my own question, but interested in opinions.

how to convert exponential string to normal string in c#

I am reading a column number from csv where the number is 123456791234567 however due to format it gets converted to
number="1.23457E+14" in the csv file.
is there any way where we can change it to original string using c# ?
I am trying with below code :
decimal number = Decimal.Parse(number,System.Globalization.NumberStyles.Any);
but the number i am getting is 123457000000000M and the actual number is 123456791234567
any idea on this?
If I understand correctly, excel is converting 123456791234567 to 1.23457E+14. In that case, you just need to format the excel cell (or potentially the column) containing the value to string, before you set the value.
If the C# program opens the csv to find the value to be 1.23457E+14, then there is no way for you to convert it back to 123456791234567, since the precision is already lost - unless of course the same value exists (or can be recreated) in other cells (columns)

Devexpress Gridview how get row value in decimal type cell?

I have a grid control and I wanna get the value of the selected row. I could get the value of varchar type cell in row but I'm having trouble in getting the value for cell in decimal type.
Here's my code for every row click:
private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
{
var productPrice = Convert.ToDecimal(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "product_price").ToString());
MessageBox.Show(productPrice.ToString());
}
For more info the product_price column is set to be in decimal type with length of (10,2) so the value its displaying is like 13,233.00
Thank you I hope you can help me to solve this problem
My first thought: Is there no focused row? Add this and see if it fixes the issue:
if (gridView1.SelectedRowsCount == 0)
return;
That wasn't the issue? Okay, add this to your code and set a breakpoint:
object o = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "product_price");
Type t = o.GetType();
When you add a watch (or hover) over t, does it say System.Decimal or String?
If it's a decimal, you should be in good shape, and what I would suggest is to just change the code to the following:
var productPrice = Convert.ToDecimal(gridView1.GetRowCellValue(
gridView1.FocusedRowHandle, "product_price"));
The only thing I omitted was the ToString(), so we can let the Convert function apply against the (raw) object. I don't know how or why, and it actually seems like a long shot, but maybe ToString() is formatting the output in accordance with the grid display.
I'm guessing that is not the case, and your original supposition is correct in that the data coming from MySQL is actually formatted as a string, or your C# code is reading it as a string. If this is the case, run the actual SQL and see what datatype is being rendered. Is it numeric? If so, is your C# reading it as numeric (reader.GetDecimal(x)) or, perhaps is there a reader.GetValue(x).ToString() that is ignoring datatypes?
In other words, is MySQL adding the formatting and sending a string, or is your C# changing a decimal to a string? Find out what's doing that and put a stop to it.
You can certainly unformat the string and then convert to a decimal, but that seems like a long, LONG path to your end goal.

Values storable in Excel

Good evening!
Which types of values can be directly stored into an Excel worksheet using Range.Value2 and how do I quickly check if a particular value can?
Suppose I have an array of objects, perhaps multityped (e.g. one int, one double and one Foo stored in an object[]).
If I shall choose a range of width 3 and try to store this array using Range.Value2, this will result in an exception (of course Excel doesn't know what is a Foo).
I came up with an idea of checking each value in the array, and, if it's not storable, convert it to its string representation using ToString(). But how do I check if it's initially storable?
It would be horrible to end up doing something like that:
public bool storable<T>(T value)
{
return value is int ||
value is uint ||
value is short ||
value is byte ||
...
value is string;
}
...especially knowing that each is will cast the variable to the tested type and seriously affect performance.
On the other hand, I can't afford pre-casting each value to the string type as I sometimes want to be able to do graphs and diagrams with numeric values, not strings.
Can you tell me I am mistaken or offer me any solution to the problem?
Thank you!
I think you're going to have to do what you're unkeen to do (all the "is" checks), unless you can somehow make your input array a bit more strongly typed. Your best bet might be just to order the casts such that the most common ones get hit first.

Determining the field length of a column in a datatable

I have a DataTable that I have populated via the Dataset.ReadXML() method. I am trying to programatically determine the maximum field lengths for each column in the datatable. The MaxLength value for all of my columns is always at the default of -1.
Any thoughts or examples on how to determine the proper max length? maybe based on the actual data in the table? (Datatable can be in the 25 column by 200,000+ row range)
C# 2.0
I don't quite understand what your objectives are - what are you trying to find out? The number of bytes a given column will use (e.g. 4 for INT, 8 for BIGINT and so forth), or the actual current maximum length of e.g. all strings in column "ColA" ?
As for the INT and other numerical and boolean types - those have a fixed system-given length - no problems there.
Unless your XML has a schema (XSD file) which limits the string lengths, the string fields from an XML can be any length, really, so after reading them in, your DataTable can't really know what the defined max length can be.
All you can do is loop over all rows in your DataTable and determine the current length of the strings, and get the maximum of those current lengths, as your frame of reference.
Does that help at all?
Marc
I believe it would be DataSet.DataTable.Columns[0].MaxLength

Categories