How to get the index count from a DatetimeIndex by given the date? - python-3.x

I have a DataFrame and its index is the type of DatetimeIndex and it looks as follow:
DatetimeIndex(
['2003-10-17', '2003-10-21', '2003-10-22', '2003-10-23',
'2003-10-24', '2003-10-27', '2003-10-28', '2003-10-29',
'2003-10-30', '2003-10-31',
...
'2017-08-04', '2017-08-07', '2017-08-08', '2017-08-09',
'2017-08-10', '2017-08-11', '2017-08-14', '2017-08-15',
'2017-08-16', '2017-08-17'
],
dtype='datetime64[ns, UTC]', name=u'DATE', length=3482, freq=None
)
I wonder how to get the position of index-count of 2017-08-04 for example.

To get just the integer position of key '2017-08-04' use DatetimeIndex.get_loc function:
dt_idx = pd.DatetimeIndex(
[ '2003-10-17', '2003-10-21', '2003-10-22', '2003-10-23', '2003-10-24', '2003-10-27',
'2003-10-28', '2003-10-29', '2003-10-30', '2003-10-31', '2017-08-04', '2017-08-07',
'2017-08-08', '2017-08-09', '2017-08-10', '2017-08-11', '2017-08-14', '2017-08-15',
'2017-08-16', '2017-08-17'
], dtype='datetime64[ns, UTC]', name=u'DATE', length=3482, freq=None)
print(dt_idx.get_loc('2017-08-04'))

Related

Convert a list of 3D coordinates in string format to a list of floats

I have a list of 3D coordinates in the format as list_X.
list_X =' [43.807 7.064 77.155], [35.099 3.179 82.838], [53.176052 -5.4618497 83.53082 ], [39.75858 1.5679997 74.76174 ], [42.055664 2.459083 80.89183 ]'
I want to convert into floats as below
list_X =[43.807 7.064 77.155], [35.099 3.179 82.838], [53.176052 -5.4618497 83.53082 ], [39.75858 1.5679997 74.76174 ], [42.055664 2.459083 80.89183 ]
I was trying as below which doesn't work
list1=[float(x) for x in list_X]
You can clean up the string to fit in the format of a list (i.e., add surrounding square brackets ([]) to contain all of the 3D coordinates, and separate the values by commas), and then use the json.loads method.
import json
list_X ='[[43.807, 7.064, 77.155], [35.099, 3.179, 82.838], [53.176052, -5.4618497, 83.53082], [39.75858, 1.5679997, 74.76174], [42.055664, 2.459083, 80.89183]]'
print(json.loads(list_X))
# Output
[[43.807, 7.064, 77.155], [35.099, 3.179, 82.838], [53.176052, -5.4618497, 83.53082], [39.75858, 1.5679997, 74.76174], [42.055664, 2.459083, 80.89183]]

Convert an array of array to array of JSON

I have an array of arrays such as this:
pl = [
["name1", "address1"],
["name2", ["address2"],
["name3", "address3"]
....
]
but I need to convert it into an array of objects:
pl = [
{"name1": "address1"},
{"name2": ["address2"},
{"name3": "address3"}
....
]
I'm struggling, with no luck.
Docs: https://docs.python.org/3/library/json.html
Example from docs:
import json
json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])

MSSQL module converting return value to int?

We are using the MSSQL module with Node.js.
I am running the following query:
SELECT AVG((RAT_VALUE * 1.0)) FROM RAT WHERE RAT_PER_ID_FROM IS NOT NULL AND RAT_PER_ID_ABOUT = 139 AND RAT_USE = 'Y' AND RAT_ABOUT_ROLE = 'RS' AND RAT_DATE_INSERTED >= '10/1/2018' AND RAT_DATE_INSERTED < '10/1/2019'
If I run this against the database directly, it returns:
4.45
The output from MSSQL is:
4
The exact resultset returned is:
results { recordsets: [ [ [Object] ] ],
recordset: [ { '': 4 } ],
output: {},
rowsAffected: [ 1 ] }
In other words, MSSQL is always returning the value 4, instead of 4.45.
The column type od RAT_VALUE is INT in the database but I've tried changing it to DECIMAL(5, 2) without any luck.
I've tried explicitly returning a DECIMAL from the query like:
SELECT CAST(AVG((RAT_VALUE * 1.0)) AS DECIMAL(5, 2)) ...
But no luck there either.
It seems MSSQL is simply clipping and dropping the decimal part of any number, even numbers of Decimal types.
I even set the value as 4.75 in the database and returned it directly and it still returns 4.
Any ideas out there?

To check if the continuity of dates are missing in a column

I want to check in my dataframe's column that if there is a missing date for a certain month then the code should output the following month in the format MMM- YYYY
The data set looks like this :
date_start_balance date_end_balance start_balance
22.02.16 22.03.16 3590838
22.04.16 22.05.16 69788
15.06.16 21.07.16 452165
Both date cols are in datetime format. Now in the above data set the dates are missing for March and May in the start col and this should be returned as MMM-YYYYY
I have tried the following code :
import datetime
dates = df1['date_start_balance'].tolist()
missing = []
for i in range(0,len(dates)-1):
if dates[i+1].month - dates[i+1].month != 1:
for j in range(dates[i].month+1,dates[i+1].month):
missing.append(datetime(dates[i].year, j,1))
print(missing)
You can first create a date range with pd.date_range
march = pd.date_range(start='2016-05-01', end='2016-05-31')
And then you will have the list with the dates that you already have, in the example there is only one date: 2016-05-15:
your_list = [datetime.datetime.strptime('15052016', "%d%m%Y").date()]
And then you can calculate the difference between the range and your list and get the dates that you are missing:
march.difference(your_list)
DatetimeIndex(['2016-05-01', '2016-05-02', '2016-05-03', '2016-05-04',
'2016-05-05', '2016-05-06', '2016-05-07', '2016-05-08',
'2016-05-09', '2016-05-10', '2016-05-11', '2016-05-12',
'2016-05-13', '2016-05-14', '2016-05-16', '2016-05-17',
'2016-05-18', '2016-05-19', '2016-05-20', '2016-05-21',
'2016-05-22', '2016-05-23', '2016-05-24', '2016-05-25',
'2016-05-26', '2016-05-27', '2016-05-28', '2016-05-29',
'2016-05-30', '2016-05-31'],
dtype='datetime64[ns]', freq=None)

Python 3: Accesing values in key in dictionary

automobiles = {
'germany': ['bmw', 'audi', 'mercedes benz'],
'japan': ['honda', 'toyota', 'subaru'],
'united states': ['ford', 'gm', 'buick'],
'italy': ['alfa romeo', 'ferrari', 'maserati'],
'great britain': ['jaguar', 'mini', 'aston martin'],
}
How can I access individual values in the different keys? (example: Who do I access the 'audi' in the germany key or 'buick' in the united states key?
So you have a dictionary who's keys are strings and who's values are lists.
To access 'audi' you can do this:
print(automobiles['germany'][1])
# which prints audi
Syntax: command(dict['key'][index])
In your case this would translate to:
print(automobiles['germany'][1])
'audi'
print(automobiles['united states'][2])
'buick'
The values stored in your dictionary are stored as lists. You can access elements in a list like list[n] where n is the index of the value you wish to access. Indexes start at 0 for python.

Resources